Add vlogging.basic_config.

master
voussoir 2021-10-09 17:47:48 -07:00
parent 5b6a1b4c9c
commit e826eeca2c
No known key found for this signature in database
GPG Key ID: 5F7554F8C26DACCB
1 changed files with 23 additions and 12 deletions

View File

@ -40,6 +40,28 @@ def add_loud(log):
addLevelName(LOUD, 'LOUD')
log.loud = loud.__get__(log, log.__class__)
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)
root.addHandler(handler)
def get_level_by_argv(argv):
'''
If any of the following arguments are present in argv, return the
@ -139,17 +161,6 @@ def main_level_by_argv(argv):
'''
(level, argv) = get_level_by_argv(argv)
# 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)
basic_config(level)
return argv