diff options
author | Aaron LI <aaronly.me@outlook.com> | 2016-09-29 23:13:17 +0800 |
---|---|---|
committer | Aaron LI <aaronly.me@outlook.com> | 2016-09-29 23:13:17 +0800 |
commit | 11b60e1c5b9bb5292322645b84d2f5d16118d211 (patch) | |
tree | 548e6382d47722a1a2af904638c1d6e8ffb056c7 | |
parent | 89cb413cc1fe1495665acc090f286ec42bcaa5bb (diff) | |
download | fg21sim-11b60e1c5b9bb5292322645b84d2f5d16118d211.tar.bz2 |
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.
-rw-r--r-- | fg21sim/utils/logging.py | 5 |
1 files changed, 5 insertions, 0 deletions
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) |