diff options
| author | Aaron LI <aaronly.me@outlook.com> | 2016-09-29 18:31:05 +0800 | 
|---|---|---|
| committer | Aaron LI <aaronly.me@outlook.com> | 2016-09-29 18:31:05 +0800 | 
| commit | 63a46f28ade5045a04991aeec68d850154cbca1f (patch) | |
| tree | 97f77edb13cf87327a32817914d12d2db15f5a1e | |
| parent | df283a9456cadfcfdfbc1ca2246108eabfcae24c (diff) | |
| download | fg21sim-63a46f28ade5045a04991aeec68d850154cbca1f.tar.bz2 | |
setup_logging(): Add parameter "level"
Also fix a bug about parameter "stream".
| -rw-r--r-- | fg21sim/utils/logging.py | 16 | 
1 files 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`` | 
