Skip to content

Commit 5361ced

Browse files
mvanhornclaude
andcommitted
gh-145238: Add native thread ID to logging record attributes
Add a `nativeThreadId` attribute to `LogRecord` that exposes the OS-native thread ID via `threading.get_native_id()`. This is useful for correlating log entries with system tools like htop, strace, and perf that display native thread IDs. The attribute can be used in format strings as `%(nativeThreadId)d`. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 0156133 commit 5361ced

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

Doc/library/logging.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,9 @@ the options available to you.
10621062
| | | of the logging call which resulted in the |
10631063
| | | creation of this record. |
10641064
+----------------+-------------------------+-----------------------------------------------+
1065+
| nativeThreadId | ``%(nativeThreadId)d`` | Native thread ID (if available). See |
1066+
| | | :func:`threading.get_native_id`. |
1067+
+----------------+-------------------------+-----------------------------------------------+
10651068
| thread | ``%(thread)d`` | Thread ID (if available). |
10661069
+----------------+-------------------------+-----------------------------------------------+
10671070
| threadName | ``%(threadName)s`` | Thread name (if available). |
@@ -1075,6 +1078,9 @@ the options available to you.
10751078
.. versionchanged:: 3.12
10761079
*taskName* was added.
10771080

1081+
.. versionchanged:: 3.15
1082+
*nativeThreadId* was added.
1083+
10781084
.. _logger-adapter:
10791085

10801086
LoggerAdapter Objects

Lib/logging/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,14 @@ def __init__(self, name, level, pathname, lineno,
350350
if logThreads:
351351
self.thread = threading.get_ident()
352352
self.threadName = threading.current_thread().name
353+
if threading._HAVE_THREAD_NATIVE_ID:
354+
self.nativeThreadId = threading.get_native_id()
355+
else: # pragma: no cover
356+
self.nativeThreadId = None
353357
else: # pragma: no cover
354358
self.thread = None
355359
self.threadName = None
360+
self.nativeThreadId = None
356361
if not logMultiprocessing: # pragma: no cover
357362
self.processName = None
358363
else:
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Added ``nativeThreadId`` attribute to :class:`logging.LogRecord`, exposing
2+
the OS-native thread ID via :func:`threading.get_native_id`. This can be
3+
used in format strings as ``%(nativeThreadId)d``.

0 commit comments

Comments
 (0)