diff options
author | Aaron LI <aaronly.me@outlook.com> | 2016-09-29 22:58:54 +0800 |
---|---|---|
committer | Aaron LI <aaronly.me@outlook.com> | 2016-09-29 22:58:54 +0800 |
commit | 89cb413cc1fe1495665acc090f286ec42bcaa5bb (patch) | |
tree | a31468254b549eb58eae050842face3c98f0d122 /fg21sim/utils/logging.py | |
parent | 20081ab8a88a42e57644d729345ec75aee3da132 (diff) | |
download | fg21sim-89cb413cc1fe1495665acc090f286ec42bcaa5bb.tar.bz2 |
utils/logging: reset handlers before initialization/reconfiguration
Also improve the comments.
Diffstat (limited to 'fg21sim/utils/logging.py')
-rw-r--r-- | fg21sim/utils/logging.py | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/fg21sim/utils/logging.py b/fg21sim/utils/logging.py index 0abf4d3..2630a58 100644 --- a/fg21sim/utils/logging.py +++ b/fg21sim/utils/logging.py @@ -19,6 +19,7 @@ def setup_logging(dict_config=None, level=None, stream=None, logfile=None): ---------- dict_config : dict Dict of logging configurations specified in the config file. + If this parameter specified, the logging will be reconfigured. level : string; Override the existing log level stream : string; "stderr", "stdout", or "" @@ -37,16 +38,22 @@ def setup_logging(dict_config=None, level=None, stream=None, logfile=None): """ # default file open mode for logging to file filemode = "a" + root_logger = logging.getLogger() # if dict_config: # XXX: # "basicConfig()" does NOT accept paramter "filemode" if the # corresponding parameter "filename" NOT specified. filemode = dict_config.pop("filemode", filemode) + # Clear existing handlers, otherwise further "basicConfig" calls + # will be ignored + for handler in root_logger.handlers: + root_logger.removeHandler(handler) + # Initialize/reconfigure the logging, which will automatically + # create a ``Formatter`` for handlers if necessary, and adding + # the handlers to the "root" logger. logging.basicConfig(**dict_config) # - root_logger = logging.getLogger() - # if level is not None: level_int = getattr(logging, level.upper(), None) if not isinstance(level_int, int): |