Charlie
Charlie

Reputation: 1892

show thread native_id in python logging

is there a way to show the thread native_id:

native_id

The native integral thread ID of this thread. This is a non-negative integer, or None if the thread has not been started. See the get_native_id() function. This represents the Thread ID (TID) as assigned to the thread by the OS (kernel). Its value may be used to uniquely identify this particular thread system-wide (until the thread terminates, after which the value may be recycled by the OS).

in the python logs using the logging LogRecord attributes.

%(threadName)s and %(thread)s do not show the native_id.

I am using linux Ubuntu and RHEL.

Thanks

Upvotes: 4

Views: 3397

Answers (1)

Ron Serruya
Ron Serruya

Reputation: 4426

You can add a filter function that intercepts log messages and adds the thread id to them

In [1]: import threading

In [2]: def thread_id_filter(record):
   ...:     """Inject thread_id to log records"""
   ...:     record.thread_id = threading.get_native_id()
   ...:     return record
   ...:

In [3]: import logging

In [4]: my_logger = logging.getLogger()

In [5]: handler = logging.StreamHandler()
# changed formatting character from `s` to `d` for thread_id
In [6]: handler.setFormatter(logging.Formatter('%(asctime)s | %(levelname)s | %(thread_id)d | %(message)s'))

In [7]: handler.addFilter(thread_id_filter)

In [8]: my_logger.addHandler(handler)

In [9]: my_logger.setLevel('INFO')

In [10]: my_logger.info('test123')
2021-06-15 18:33:20,400 | INFO | 6795172 | test123

Upvotes: 7

Related Questions