python / python/cpython

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

Open
#145,238 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Feature or enhancement

Proposal:

It would be nice to have the native thread ID be one of the options for the log record attributes for python logging if at all possible.
https://docs.python.org/3/library/logging.html#logrecord-attributes

I came across this just now while forming a custom formatter class class SpecialLogFormatter(logging.Formatter): and passing into its constructor a flag to expose multiprocess details or to skip those using an unevaluated format string kept as member data. When I call the method def format(self, record: logging.LogRecord) -> str: I had to still conditionally check the process detail logging state because I need to pass in a separate variable to pass the threading.get_native_id information. Because the format string in the False case for the flag does not have that field in the format string to be rendered I'd get a key error in that case so I have to conditionally add it to the kwargs for the str.format invocation. I could avoid this per format invocation if the record object itself had the native thread ID present.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs
  • gh-145655

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with Python's logging LogRecord attributes and Formatter.format behavior, then review linked PR gh-145655 and the threading.get_native_id entry point. Done means the native thread ID is available as a selectable log record attribute without breaking format strings that omit it.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
observability
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.