mirror of
https://github.com/EDCD/EDMarketConnector.git
synced 2025-04-18 18:07:37 +03:00
added trace-if and TRACE_ALL loglevels
This commit is contained in:
parent
505692052e
commit
41895d591a
@ -48,6 +48,7 @@ from threading import get_native_id as thread_native_id
|
|||||||
from traceback import print_exc
|
from traceback import print_exc
|
||||||
from typing import TYPE_CHECKING, Tuple, cast
|
from typing import TYPE_CHECKING, Tuple, cast
|
||||||
|
|
||||||
|
import config as config_mod
|
||||||
from config import appcmdname, appname, config
|
from config import appcmdname, appname, config
|
||||||
|
|
||||||
# TODO: Tests:
|
# TODO: Tests:
|
||||||
@ -77,8 +78,11 @@ _default_loglevel = logging.DEBUG
|
|||||||
|
|
||||||
# Define a TRACE level
|
# Define a TRACE level
|
||||||
LEVEL_TRACE = 5
|
LEVEL_TRACE = 5
|
||||||
|
LEVEL_TRACE_ALL = 3
|
||||||
logging.addLevelName(LEVEL_TRACE, "TRACE")
|
logging.addLevelName(LEVEL_TRACE, "TRACE")
|
||||||
|
logging.addLevelName(LEVEL_TRACE_ALL, "TRACE_ALL")
|
||||||
logging.TRACE = LEVEL_TRACE # type: ignore
|
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.Logger.trace = lambda self, message, *args, **kwargs: self._log( # type: ignore
|
||||||
logging.TRACE, # type: ignore
|
logging.TRACE, # type: ignore
|
||||||
message,
|
message,
|
||||||
@ -86,6 +90,21 @@ logging.Logger.trace = lambda self, message, *args, **kwargs: self._log( # type
|
|||||||
**kwargs
|
**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:
|
if TYPE_CHECKING:
|
||||||
from types import FrameType
|
from types import FrameType
|
||||||
|
|
||||||
@ -98,6 +117,13 @@ if TYPE_CHECKING:
|
|||||||
"""Fake trace method."""
|
"""Fake trace method."""
|
||||||
return self._log(LEVEL_TRACE, message, args, **kwargs)
|
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:
|
class Logger:
|
||||||
"""
|
"""
|
||||||
@ -492,7 +518,7 @@ class EDMCContextFilter(logging.Filter):
|
|||||||
return module_name
|
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."""
|
"""Return the correct logger for how the program is being run."""
|
||||||
if not os.getenv("EDMC_NO_UI"):
|
if not os.getenv("EDMC_NO_UI"):
|
||||||
# GUI app being run
|
# GUI app being run
|
||||||
|
Loading…
x
Reference in New Issue
Block a user