Setup non-law loggers in logging setup, closes
@@ -22,8 +22,10 @@ console_handler = None
def setup_logging():
"""
Sets up the internal logging mechanism, i.e., it creates the :py:attr:`console_handler`, sets
- its formatting, and mounts on on the main ``"law"`` logger. It also sets the levels of all
- loggers that are given in the law config.
+ its formatting, and adds it to the main logger which propagates settings to lower level loggers.
+ In addition, all other loggers listed in the ``"logging"`` config section as (*name*, *level*)
+ pairs are set up. This includes loggers that do not use the ``"law.*"`` namespace which can be
+ seen as a convenient feature to set up custom loggers.
"""
global console_handler
@@ -31,17 +33,23 @@ def setup_logging():
if console_handler:
return
- # set the handler of the law root logger
+ # set the handler of the law root logger which propagates it to lower level loggers
console_handler = logging.StreamHandler()
console_handler.setFormatter(LogFormatter())
logging.getLogger("law").addHandler(console_handler)
- # set levels for all loggers
+ # set levels for all loggers and add the console handler for all non-law loggers
for name, level in Config.instance().items_expanded("logging"):
level = level.upper()
if hasattr(logging, level):
+ # create / get the logger and set the level
logger = logging.getLogger(name)
logger.setLevel(getattr(logging, level))
+
+ # add the console handler when not part of the law.* namespace
+ if not name.startswith("law."):
+ logger.addHandler(console_handler)
+
logger.debug("registered logger with level '{}'".format(level))
