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.
This commit is contained in:
voussoir 2021-05-16 21:02:04 -07:00
parent cb4cef9339
commit 03bcded66a
No known key found for this signature in database
GPG key ID: 5F7554F8C26DACCB

View file

@ -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)