From 59f7104dce31e4983b5b82773c6102eeafd4c761 Mon Sep 17 00:00:00 2001 From: Unrud Date: Thu, 16 Aug 2018 08:00:02 +0200 Subject: [PATCH] Replace option "debug" with "level" in "logging" --- config | 5 +++-- radicale/__init__.py | 2 +- radicale/__main__.py | 9 +++++++-- radicale/config.py | 15 ++++++++++----- radicale/log.py | 16 ++++++++-------- 5 files changed, 29 insertions(+), 18 deletions(-) diff --git a/config b/config index 43a9254..d724a88 100644 --- a/config +++ b/config @@ -125,8 +125,9 @@ [logging] -# Set the logging level to debug -#debug = False +# Threshold for the logger +# Value: debug | info | warning | error | critical +#level = warning # Don't include passwords in logs #mask_passwords = True diff --git a/radicale/__init__.py b/radicale/__init__.py index 4f5af76..71373e5 100644 --- a/radicale/__init__.py +++ b/radicale/__init__.py @@ -850,7 +850,7 @@ def _init_application(config_path, wsgi_errors): _application_config_path = config_path configuration = config.load([config_path] if config_path else [], ignore_missing_paths=False) - log.set_debug(configuration.getboolean("logging", "debug")) + log.set_level(configuration.get("logging", "level")) _application = Application(configuration) diff --git a/radicale/__main__.py b/radicale/__main__.py index ba7ee71..43654a9 100644 --- a/radicale/__main__.py +++ b/radicale/__main__.py @@ -41,6 +41,8 @@ def run(): help="check the storage for errors and exit") parser.add_argument( "-C", "--config", help="use a specific configuration file") + parser.add_argument("-D", "--debug", action="store_true", + help="print debug information") groups = {} for section, values in config.INITIAL_CONFIG.items(): @@ -76,7 +78,10 @@ def run(): args = parser.parse_args() # Preliminary configure logging - log.set_debug(args.logging_debug) + if args.debug: + args.logging_level = "debug" + if args.logging_level is not None: + log.set_level(args.logging_level) if args.config is not None: config_paths = [args.config] if args.config else [] @@ -103,7 +108,7 @@ def run(): configuration.set(section, action.split('_', 1)[1], value) # Configure logging - log.set_debug(configuration.getboolean("logging", "debug")) + log.set_level(configuration.get("logging", "level")) if args.verify_storage: logger.info("Verifying storage") diff --git a/radicale/config.py b/radicale/config.py index fd3b208..d9378d3 100644 --- a/radicale/config.py +++ b/radicale/config.py @@ -49,6 +49,12 @@ def positive_float(value): return value +def logging_level(value): + if value not in ("debug", "info", "warning", "error", "critical"): + raise ValueError("unsupported level: %s" % value) + return value + + # Default configuration INITIAL_CONFIG = OrderedDict([ ("server", OrderedDict([ @@ -173,11 +179,10 @@ INITIAL_CONFIG = OrderedDict([ "type": str, "internal": web.INTERNAL_TYPES})])), ("logging", OrderedDict([ - ("debug", { - "value": "False", - "help": "print debug information", - "aliases": ["-D", "--debug"], - "type": bool}), + ("level", { + "value": "warning", + "help": "threshold for the logger", + "type": logging_level}), ("mask_passwords", { "value": "True", "help": "mask passwords in logs", diff --git a/radicale/log.py b/radicale/log.py index 7617ce0..0304131 100644 --- a/radicale/log.py +++ b/radicale/log.py @@ -116,16 +116,16 @@ def setup(): handler = ThreadStreamsHandler(sys.stderr, get_default_handler()) logging.basicConfig(format=LOGGER_FORMAT, handlers=[handler]) register_stream = handler.register_stream - set_debug(True) + set_level(logging.DEBUG) -def set_debug(debug): - """Set debug mode for global logger.""" - if debug: - root_logger.setLevel(logging.DEBUG) - logger.setLevel(logging.DEBUG) +def set_level(level): + """Set logging level for global logger.""" + if isinstance(level, str): + level = getattr(logging, level.upper()) + root_logger.setLevel(level) + logger.setLevel(level) + if level == logging.DEBUG: logger.removeFilter(removeTracebackFilter) else: - root_logger.setLevel(logging.WARNING) - logger.setLevel(logging.WARNING) logger.addFilter(removeTracebackFilter)