From 4c9cf9ed659df6f08c9c7e328d756413913a3595 Mon Sep 17 00:00:00 2001 From: Athanasius Date: Mon, 22 Mar 2021 12:53:56 +0000 Subject: [PATCH] Logging: Add osthreadid, and add process, thread and osthreadid to format * It looks like in Python 3.8 the %(thread)d is the native OS thread ID anyway, but let's keep osthreadid anyway. --- EDMCLogging.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/EDMCLogging.py b/EDMCLogging.py index b52c0ab7..8445a9fe 100644 --- a/EDMCLogging.py +++ b/EDMCLogging.py @@ -43,7 +43,8 @@ import pathlib import tempfile # So that any warning about accessing a protected member is only in one place. from sys import _getframe as getframe -from typing import TYPE_CHECKING, Tuple, cast +from threading import get_native_id as thread_native_id +from typing import Tuple from config import appcmdname, appname, config @@ -127,7 +128,7 @@ class Logger: # This should be affected by the user configured log level self.logger_channel.setLevel(loglevel) - self.logger_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(module)s.%(qualname)s:%(lineno)d: %(message)s') # noqa: E501 + self.logger_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(process)d:%(thread)d:%(osthreadid)d %(module)s.%(qualname)s:%(lineno)d: %(message)s') # noqa: E501 self.logger_formatter.default_time_format = '%Y-%m-%d %H:%M:%S' self.logger_formatter.default_msec_format = '%s.%03d' @@ -251,6 +252,7 @@ class EDMCContextFilter(logging.Filter): logging.Formatter() as you can use just this no matter if there is a class involved or not, so you get a nice clean: .[.classB....]. + 3. osthreadid = OS level thread ID. If we fail to be able to properly set either then: @@ -275,10 +277,12 @@ class EDMCContextFilter(logging.Filter): if getattr(record, 'qualname', None) is None: setattr(record, 'qualname', qualname) + setattr(record, 'osthreadid', thread_native_id()) + return True - @classmethod # noqa: CCR001 - this is as refactored as is sensible - def caller_attributes(cls, module_name: str = '') -> Tuple[str, str, str]: + @classmethod + def caller_attributes(cls, module_name: str = '') -> Tuple[str, str, str]: # noqa: CCR001, E501 # this is as refactored as is sensible """ Determine extra or changed fields for the caller.