Skip to content

gh-145238: Add native thread ID to logging record attributes#145655

Open
mvanhorn wants to merge 1 commit intopython:mainfrom
mvanhorn:osc/145238-add-native-thread-id-logging
Open

gh-145238: Add native thread ID to logging record attributes#145655
mvanhorn wants to merge 1 commit intopython:mainfrom
mvanhorn:osc/145238-add-native-thread-id-logging

Conversation

@mvanhorn
Copy link

@mvanhorn mvanhorn commented Mar 9, 2026

Fixes #145238.

Adds 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.

On platforms where get_native_id is not available, the attribute is set to None.

This contribution was developed with AI assistance (Claude Code).


📚 Documentation preview 📚: https://cpython-previews--145655.org.readthedocs.build/

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>
@mvanhorn mvanhorn requested a review from vsajip as a code owner March 9, 2026 03:38
@python-cla-bot
Copy link

python-cla-bot bot commented Mar 9, 2026

All commit authors signed the Contributor License Agreement.

CLA signed

@vsajip
Copy link
Member

vsajip commented Mar 9, 2026

Just curious, @mvanhorn, why did you need to use Claude Code for this tiny change?

@mvanhorn
Copy link
Author

mvanhorn commented Mar 9, 2026

@vsajip Fair question! I use Claude Code as my daily coding environment - it's how I navigate codebases, run tests, and write code. So even for smaller changes like this, it's just part of my normal workflow rather than something I specifically "needed" AI for. The implementation decisions (checking _HAVE_THREAD_NATIVE_ID, the attribute placement, docs/NEWS entry) were all my own - Claude Code is just the editor I happen to work in.

@vsajip vsajip added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Mar 9, 2026
@bedevere-bot
Copy link

🤖 New build scheduled with the buildbot fleet by @vsajip for commit 5361ced 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F145655%2Fmerge

If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Mar 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add threading.get_native_id as an available log record attribute to choose from.

3 participants