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

added trace-if and TRACE_ALL loglevels

This commit is contained in:
A_D 2021-08-12 16:45:18 +02:00
parent 505692052e
commit 41895d591a
No known key found for this signature in database
GPG Key ID: 4BE9EB7DF45076C4

View File

@ -48,6 +48,7 @@ from threading import get_native_id as thread_native_id
from traceback import print_exc
from typing import TYPE_CHECKING, Tuple, cast
import config as config_mod
from config import appcmdname, appname, config
# TODO: Tests:
@ -77,8 +78,11 @@ _default_loglevel = logging.DEBUG
# Define a TRACE level
LEVEL_TRACE = 5
LEVEL_TRACE_ALL = 3
logging.addLevelName(LEVEL_TRACE, "TRACE")
logging.addLevelName(LEVEL_TRACE_ALL, "TRACE_ALL")
logging.TRACE = LEVEL_TRACE # type: ignore
logging.TRACE_ALL = LEVEL_TRACE_ALL # type: ignore
logging.Logger.trace = lambda self, message, *args, **kwargs: self._log( # type: ignore
logging.TRACE, # type: ignore
message,
@ -86,6 +90,21 @@ logging.Logger.trace = lambda self, message, *args, **kwargs: self._log( # type
**kwargs
)
def _trace_if(self: logging.Logger, condition: str, message: str, *args, **kwargs) -> None:
if condition not in config_mod.trace_on:
self._log(logging.TRACE_ALL, message, args, **kwargs) # type: ignore # we added it
else:
# its in there, it gets to end up in regular TRACE
self._log(logging.TRACE, message, args, **kwargs) # type: ignore # we added it
logging.Logger.trace_if = _trace_if # type: ignore
# we cant hide this from `from xxx` imports and I'd really rather no-one other than `logging` had access to it
del _trace_if
if TYPE_CHECKING:
from types import FrameType
@ -98,6 +117,13 @@ if TYPE_CHECKING:
"""Fake trace method."""
return self._log(LEVEL_TRACE, message, args, **kwargs)
def trace_if(self, condition: str, message, *args, **kwargs) -> None:
"""Fake trace if method, traces only if condition exists in trace_on."""
if condition in config_mod.trace_on:
return self._log(LEVEL_TRACE, message, *args, **kwargs)
else:
return self._log(LEVEL_TRACE_ALL, message, *args, **kwargs)
class Logger:
"""
@ -492,7 +518,7 @@ class EDMCContextFilter(logging.Filter):
return module_name
def get_main_logger() -> 'LoggerMixin':
def get_main_logger(sublogger_name: str = '') -> 'LoggerMixin':
"""Return the correct logger for how the program is being run."""
if not os.getenv("EDMC_NO_UI"):
# GUI app being run