From 04c4f5e6834907811e242c682119e4479eafdad7 Mon Sep 17 00:00:00 2001 From: Athanasius Date: Thu, 30 Jul 2020 16:29:43 +0100 Subject: [PATCH] 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. --- EDMCLogging.py | 24 ++++++++++++++++-------- EDMarketConnector.py | 2 +- plug.py | 2 +- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/EDMCLogging.py b/EDMCLogging.py index 7ad069a1..ecb96050 100644 --- a/EDMCLogging.py +++ b/EDMCLogging.py @@ -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 = '' 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 \ No newline at end of file diff --git a/EDMarketConnector.py b/EDMarketConnector.py index 2d1b81b9..c1005a80 100755 --- a/EDMarketConnector.py +++ b/EDMarketConnector.py @@ -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): diff --git a/plug.py b/plug.py index 0a0a3f8a..8731a173 100644 --- a/plug.py +++ b/plug.py @@ -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