Replace call to basicConfig with own handler.

master
voussoir 2021-06-21 21:53:36 -07:00
parent 689b8f5718
commit 9cd7713a6b
No known key found for this signature in database
GPG Key ID: 5F7554F8C26DACCB
1 changed files with 16 additions and 6 deletions

View File

@ -107,14 +107,24 @@ def get_level_by_name(name):
def main_level_by_argv(argv): def main_level_by_argv(argv):
''' '''
This function calls basicConfig to initialize the root logger, sets the This function puts a handler on the root logger with a level set by the
root log's level by the flags in argv, then returns the rest of argv which flags in argv, then returns the rest of argv which you can pass to
you can pass to your argparser. your argparser.
''' '''
basicConfig()
(level, argv) = get_level_by_argv(argv) (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 return argv