We have a few specific python modules which have most of their log traces of the form logging.debug
. The owner of that source wishes the log traces to be debug
, another team wishes them to be info
. The root logger the second team uses sets logging.INFO
as their application’s logging level since there are a lot of other source modules which would flood the app’s logs with debug traces if the root logger were set to that level.
Is there a way to remap log traces from a specific module to promote them from debug to info that can satisfy both clients of this code?
here’s my unsuccessful attempt at this, I effectively promote the log level of the child logger but the root logger still recognizes the log traces as debug and filters them out
# Test module: first.py
import logging
from second import symbol, log_func
root_logger = logging.getLogger()
logger = logging.getLogger(__name__)
def setup_logger() :
root_logger.setLevel(logging.INFO)
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.INFO)
info_formatter = logging.Formatter( "[%(levelname)s] %(pathname)s::%(funcName)s %(message)s" )
stream_handler.setFormatter(info_formatter)
root_logger.addHandler(stream_handler)
root_logger.info("Root Logging initialized")
if __name__ == "__main__":
setup_logger()
logger.info(symbol)
second_logger = logging.getLogger("second")
logger.info(f"imported module log level before: {second_logger.level}")
second_logger.setLevel(logging.DEBUG)
logger.info(f"imported module log level after: {second_logger.level}")
log_func()
#==================================================================================
# Imported module: second.py
import logging
logger = logging.getLogger(__name__)
symbol = "second_mod"
def log_func():
logger.info(f"Log traces coming from module '{__name__}'")
logger.debug("this is debug level") # <--------- how to get this trace visible??
logger.info("this is info level")
this code yields the following console output
$ python first.py
[INFO] first.py::setup_logger Root Logging initialized
[INFO] first.py::<module> second_mod
[INFO] first.py::<module> imported module log level before: 0
[INFO] first.py::<module> imported module log level after: 10
[INFO] second.py::log_func Log traces coming from module 'second'
[INFO] second.py::log_func this is info level
I know the logging hierarchy bubbles all the log traces from down below up to the root logger, which filters from above it all. However is there a way to effectively remap the log traces that are statically authored in a given module over to something else?