From 03bcded66a23bdd8c8a4c295d06a3200903e2ddc Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Sun, 16 May 2021 21:02:04 -0700 Subject: [PATCH] Add main_level_by_argv, the next iteration of set_level_by_argv. I have realized that when passing log levels by argv, it's better for them to go to the root logger instead of the module logger, since that would not affect the logs that come from imported modules. --- voussoirkit/vlogging.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/voussoirkit/vlogging.py b/voussoirkit/vlogging.py index a00caf7..222c918 100644 --- a/voussoirkit/vlogging.py +++ b/voussoirkit/vlogging.py @@ -105,15 +105,24 @@ def get_level_by_name(name): return value -def set_level_by_argv(log, argv): +def main_level_by_argv(argv): ''' - This function is helpful for single-file scripts where you instantiate the - logger in the global scope, and use this function to set its level - according to the "--debug" flags in argv, then pass the rest of argv to - your argparser. + 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. ''' basicConfig() + (level, argv) = get_level_by_argv(argv) + getLogger().setLevel(level) + + return argv + +def set_level_by_argv(log, argv): + ''' + This function sets the log's level by the flags in argv, then returns the + rest of argv which you can pass to your argparser. + ''' (level, argv) = get_level_by_argv(argv) log.setLevel(level)