1
0
mirror of https://github.com/EDCD/EDMarketConnector.git synced 2025-04-15 00:30:33 +03:00

Using a LoggerAdapter to prepend a string.

1. This makes setting up logging everywhere slightly more involved.
2. If I then want to change, say, %(module)s value I'll end up needing
 to stack walk again.

So this might be better done in a filter.  But these commits for the
record, and to come back to if needs be.
This commit is contained in:
Athanasius 2020-07-30 16:29:43 +01:00
parent 898ff9fbb2
commit 04c4f5e683
3 changed files with 18 additions and 10 deletions

View File

@ -9,7 +9,7 @@ strings.
from sys import _getframe as getframe
import inspect
import logging
from typing import Tuple
from typing import Any, MutableMapping, Tuple
# TODO: Tests:
@ -55,13 +55,14 @@ class Logger:
This includes using an EDMCContextFilter to add 'class' and 'qualname'
expansions for logging.Formatter().
"""
self.logger = logging.getLogger(logger_name)
# Using a LoggerAdapter, so make the actual Logger internal
self._logger = logging.getLogger(logger_name)
# Configure the logging.Logger
self.logger.setLevel(loglevel)
self._logger.setLevel(loglevel)
# Set up filter for adding class name
self.logger_filter = EDMCContextFilter()
self.logger.addFilter(self.logger_filter)
self._logger.addFilter(self.logger_filter)
self.logger_channel = logging.StreamHandler()
self.logger_channel.setLevel(loglevel)
@ -71,16 +72,18 @@ class Logger:
self.logger_formatter.default_msec_format = '%s.%03d'
self.logger_channel.setFormatter(self.logger_formatter)
self.logger.addHandler(self.logger_channel)
self._logger.addHandler(self.logger_channel)
def get_logger(self) -> logging.Logger:
self.logger = EDMCLoggerAdapter(self._logger, {'from': self.__class__.__qualname__})
def get_logger(self) -> logging.LoggerAdapter:
"""
:return: The logging.Logger instance.
"""
return self.logger
def get_plugin_logger(name: str, loglevel: int = _default_loglevel) -> logging.Logger:
def get_plugin_logger(name: str, loglevel: int = _default_loglevel) -> logging.LoggerAdapter:
"""
'Found' plugins need their own logger to call out where the logging is
coming from, but we don't need to set up *everything* for them.
@ -104,7 +107,7 @@ def get_plugin_logger(name: str, loglevel: int = _default_loglevel) -> logging.L
plugin_logger.addFilter(EDMCContextFilter())
return plugin_logger
return EDMCLoggerAdapter(plugin_logger, {'from': __name__})
class EDMCContextFilter(logging.Filter):
@ -212,3 +215,8 @@ class EDMCContextFilter(logging.Filter):
caller_class_names = '<ERROR in EDMCLogging.caller_class_and_qualname() for "class">'
return caller_class_names, caller_qualname
class EDMCLoggerAdapter(logging.LoggerAdapter):
def process(self, msg: Any, kwargs: MutableMapping[str, Any]) -> Tuple[Any, MutableMapping[str, Any]]:
return f'ADAPTED {msg}', kwargs

View File

@ -1033,7 +1033,7 @@ if __name__ == "__main__":
logger = EDMCLogging.Logger(appname).get_logger()
# TODO: unittests in place of these
# logger.debug('Test from __main__')
logger.debug('Test from __main__')
# test_logging()
class A(object):
class B(object):

View File

@ -17,7 +17,7 @@ import myNotebook as nb # noqa: N813
from config import config, appname
import EDMCLogging
logger = logging.getLogger(appname)
logger = EDMCLogging.EDMCLoggerAdapter(logging.getLogger(appname), {'from': __name__})
# Dashboard Flags constants
FlagsDocked = 1 << 0 # on a landing pad