From d082a0fbb571bf162fe46136850a878879e83a55 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Mon, 18 Oct 2021 18:20:17 -0700 Subject: [PATCH] Finally solve vlogging/opnot interplay by just setting NOTSET always. --- voussoirkit/operatornotify.py | 7 ------- voussoirkit/vlogging.py | 21 +++++++++------------ 2 files changed, 9 insertions(+), 19 deletions(-) diff --git a/voussoirkit/operatornotify.py b/voussoirkit/operatornotify.py index 266d208..65dd0ad 100644 --- a/voussoirkit/operatornotify.py +++ b/voussoirkit/operatornotify.py @@ -243,13 +243,6 @@ def main_decorator(subject, *, log_return_value=True, **kwargs): (argv, level) = get_level_by_argv(argv) context = main_log_context(subject, level, **kwargs) - # We need to call basic_config so that operatornotify's logs have - # somewhere to go. We do this only during wrapped, not before, so - # that if the main also has vlogging.main_decorator or any other - # decorators that will prepare the logging before main is called, - # this shouldn't interfere with those. - vlogging.basic_config(vlogging.INFO) - if isinstance(context, contextlib.nullcontext): return main(argv) diff --git a/voussoirkit/vlogging.py b/voussoirkit/vlogging.py index 07839a1..f2c6d6d 100644 --- a/voussoirkit/vlogging.py +++ b/voussoirkit/vlogging.py @@ -12,6 +12,15 @@ from logging import * _getLogger = getLogger +# Python gives the root logger a level of WARNING. The problem is that prevents +# any handlers you add to it from receiving lower level messages. WARNING might +# be fine for the stderr handler, but you might like to have a log file +# containing everything including info and debug. +# I find that logging works best if the root logger itself doesn't have a level +# and the handlers can choose what they want. +root = getLogger() +root.setLevel(NOTSET) + LOUD = 1 SILENT = 99999999999 @@ -45,18 +54,6 @@ def basic_config(level): This adds a handler with the given level to the root logger, but only if it has no handlers yet. ''' - # Previously I was using basicConfig to prepare the handlers and then - # setting root.setLevel, but the problem is that prevents any other - # handlers on the root from receiving messages at a lower level. - # It works best if the logger itself doesn't have a level and the handlers - # can choose what they want. - root = getLogger() - - if root.handlers: - return - - root.setLevel(NOTSET) - handler = StreamHandler() handler.setFormatter(Formatter('{levelname}:{name}:{message}', style='{')) handler.setLevel(level)