From 11b60e1c5b9bb5292322645b84d2f5d16118d211 Mon Sep 17 00:00:00 2001 From: Aaron LI Date: Thu, 29 Sep 2016 23:13:17 +0800 Subject: utils/logging.py: Save existing formatter for new handlers By saving the existing formatter and setting for the new handlers, the configured format styles are kept. Otherwise, the newly created handlers does not have a formatter, thus the default format style gets used. --- fg21sim/utils/logging.py | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'fg21sim/utils') diff --git a/fg21sim/utils/logging.py b/fg21sim/utils/logging.py index 2630a58..df0a4df 100644 --- a/fg21sim/utils/logging.py +++ b/fg21sim/utils/logging.py @@ -54,6 +54,9 @@ def setup_logging(dict_config=None, level=None, stream=None, logfile=None): # the handlers to the "root" logger. logging.basicConfig(**dict_config) # + # Configured logging has at least one handler with configured formatter. + # Keep the existing formatter to keep the configured format styles. + formatter = root_logger.handlers[0].formatter if level is not None: level_int = getattr(logging, level.upper(), None) if not isinstance(level_int, int): @@ -73,6 +76,7 @@ def setup_logging(dict_config=None, level=None, stream=None, logfile=None): else: # add new ``StreamHandler`` handler = StreamHandler(getattr(sys, stream)) + handler.setFormatter(formatter) root_logger.addHandler(handler) else: raise ValueError("invalid stream: %s" % stream) @@ -89,4 +93,5 @@ def setup_logging(dict_config=None, level=None, stream=None, logfile=None): else: # add new ``FileHandler`` handler = FileHandler(logfile, mode=filemode) + handler.setFormatter(formatter) root_logger.addHandler(handler) -- cgit v1.2.2