1
0
mirror of https://github.com/EDCD/EDMarketConnector.git synced 2025-04-13 07:47:14 +03:00

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.
This commit is contained in:
Athanasius 2021-03-22 12:53:56 +00:00
parent 7efd1a5aaa
commit 4c9cf9ed65

View File

@ -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:
<file/module>.<classA>[.classB....].<function>
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.