Skip to content

Commit 24408c3

Browse files
committed
Output thread IDs depending on process pid
Signed-off-by: Vadim Markovtsev <vadim@poolside.ai>
1 parent e2156f3 commit 24408c3

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

flogging/flogging.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,17 @@ def format_datetime(dt: datetime.datetime):
9595
return dt.strftime("%Y-%m-%dT%k:%M:%S.%f000") + tzstr
9696

9797

98+
_pid: int = os.getpid()
99+
100+
101+
def _change_pid():
102+
global _pid
103+
_pid = os.getpid()
104+
105+
98106
def reduce_thread_id(thread_id: int) -> str:
99107
"""Make a shorter thread identifier by hashing the original."""
100-
return xxhash.xxh32(thread_id.to_bytes(8, "little")).hexdigest()[:4]
108+
return xxhash.xxh32((thread_id ^ _pid).to_bytes(8, "little")).hexdigest()[:4]
101109

102110

103111
def repr_array(arr: ndarray) -> str:
@@ -324,6 +332,8 @@ def ensure_utf8_stream(stream):
324332
if not allow_trailing_dot:
325333
handler.emit = check_trailing_dot(handler.emit)
326334

335+
os.register_at_fork(after_in_child=_change_pid)
336+
327337

328338
def set_context(context):
329339
"""Assign the logging context - an abstract object - to the current thread."""

0 commit comments

Comments
 (0)