From 9cd7713a6b96e79b6d86b63508ffb16415e30f5c Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Mon, 21 Jun 2021 21:53:36 -0700 Subject: [PATCH] Replace call to basicConfig with own handler. --- voussoirkit/vlogging.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/voussoirkit/vlogging.py b/voussoirkit/vlogging.py index 222c918..fadc549 100644 --- a/voussoirkit/vlogging.py +++ b/voussoirkit/vlogging.py @@ -107,14 +107,24 @@ def get_level_by_name(name): def main_level_by_argv(argv): ''' - This function calls basicConfig to initialize the root logger, sets the - root log's level by the flags in argv, then returns the rest of argv which - you can pass to your argparser. + This function puts a handler on the root logger with a level set by the + flags in argv, then returns the rest of argv which you can pass to + your argparser. ''' - basicConfig() - (level, argv) = get_level_by_argv(argv) - getLogger().setLevel(level) + + # 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() + root.setLevel(NOTSET) + + handler = StreamHandler() + handler.setFormatter(Formatter('{levelname}:{name}:{message}', style='{')) + handler.setLevel(level) + root.addHandler(handler) return argv