From 24408c300d48545b64c558b18502a0d0a7984919 Mon Sep 17 00:00:00 2001 From: Vadim Markovtsev Date: Mon, 26 May 2025 09:24:06 +0200 Subject: [PATCH] Output thread IDs depending on process pid Signed-off-by: Vadim Markovtsev --- flogging/flogging.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/flogging/flogging.py b/flogging/flogging.py index a097f93..afce944 100644 --- a/flogging/flogging.py +++ b/flogging/flogging.py @@ -95,9 +95,17 @@ def format_datetime(dt: datetime.datetime): return dt.strftime("%Y-%m-%dT%k:%M:%S.%f000") + tzstr +_pid: int = os.getpid() + + +def _change_pid(): + global _pid + _pid = os.getpid() + + def reduce_thread_id(thread_id: int) -> str: """Make a shorter thread identifier by hashing the original.""" - return xxhash.xxh32(thread_id.to_bytes(8, "little")).hexdigest()[:4] + return xxhash.xxh32((thread_id ^ _pid).to_bytes(8, "little")).hexdigest()[:4] def repr_array(arr: ndarray) -> str: @@ -324,6 +332,8 @@ def ensure_utf8_stream(stream): if not allow_trailing_dot: handler.emit = check_trailing_dot(handler.emit) + os.register_at_fork(after_in_child=_change_pid) + def set_context(context): """Assign the logging context - an abstract object - to the current thread."""