From b347031663905b493cbf58f8348b480a802d2fdd Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Thu, 2 Feb 2023 04:36:55 +0100 Subject: [PATCH] Allow registration of multiple custom loggers --- unimore_bda_6/log.py | 50 ++++++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/unimore_bda_6/log.py b/unimore_bda_6/log.py index e01899c..1aa46a8 100644 --- a/unimore_bda_6/log.py +++ b/unimore_bda_6/log.py @@ -4,30 +4,34 @@ import coloredlogs log = logging.getLogger(__name__) -def install_log_handler(logger: logging.Logger = None): - if logger is None: - logger = logging.getLogger("unimore_bda_6") +def install_log_handler(loggers: list[logging.Logger] = None): + if loggers is None: + loggers = [ + logging.getLogger("__main__"), + logging.getLogger("unimore_bda_6"), + ] - coloredlogs.install( - logger=logger, - level="DEBUG", - fmt="{asctime} | {name:<32} | {levelname:>8} | {message}", - style="{", - level_styles=dict( - debug=dict(color="white"), - info=dict(color="cyan"), - warning=dict(color="yellow"), - error=dict(color="red"), - critical=dict(color="red", bold=True), - ), - field_styles=dict( - asctime=dict(color='magenta'), - levelname=dict(color='blue', bold=True), - name=dict(color='blue'), - ), - isatty=True, - ) - log.info("Installed custom log handler!") + for logger in loggers: + coloredlogs.install( + logger=logger, + level="DEBUG", + fmt="{asctime} | {name:<32} | {levelname:>8} | {message}", + style="{", + level_styles=dict( + debug=dict(color="white"), + info=dict(color="cyan"), + warning=dict(color="yellow"), + error=dict(color="red"), + critical=dict(color="red", bold=True), + ), + field_styles=dict( + asctime=dict(color='magenta'), + levelname=dict(color='blue', bold=True), + name=dict(color='blue'), + ), + isatty=True, + ) + log.debug("Installed custom log handler on: %s", logger) __all__ = (