From 63a46f28ade5045a04991aeec68d850154cbca1f Mon Sep 17 00:00:00 2001 From: Aaron LI Date: Thu, 29 Sep 2016 18:31:05 +0800 Subject: setup_logging(): Add parameter "level" Also fix a bug about parameter "stream". --- fg21sim/utils/logging.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/fg21sim/utils/logging.py b/fg21sim/utils/logging.py index 81e8880..2b2317f 100644 --- a/fg21sim/utils/logging.py +++ b/fg21sim/utils/logging.py @@ -10,7 +10,7 @@ import logging from logging import FileHandler, StreamHandler -def setup_logging(dict_config=None, stream=None, logfile=None): +def setup_logging(dict_config=None, level=None, stream=None, logfile=None): """Setup the logging. This will override the logging configurations in the config file if specified (e.g., by command line arguments). @@ -19,6 +19,8 @@ def setup_logging(dict_config=None, stream=None, logfile=None): ---------- dict_config : dict Dict of logging configurations specified in the config file. + level : string; + Override the existing log level stream : string; "stderr", "stdout", or "" This controls where the log messages go to. If not None, then override the old ``StreamHandler`` settings; @@ -35,12 +37,22 @@ def setup_logging(dict_config=None, stream=None, logfile=None): """ # default file open mode for logging to file filemode = "a" + # if dict_config: logging.basicConfig(**dict_config) filemode = dict_config["filemode"] # root_logger = logging.getLogger() - if stream in ["", "stderr", "stdout"]: + # + if level is not None: + level_int = getattr(logging, level.upper(), None) + if not isinstance(level_int, int): + raise ValueError("invalid log level: %s" % level) + root_logger.setLevel(level_int) + # + if stream is None: + pass + elif stream in ["", "stderr", "stdout"]: for handler in root_logger.handlers: if isinstance(handler, StreamHandler): # remove old ``StreamHandler`` -- cgit v1.2.2