Python Logging - Multiple Modules
I'm working on a small python project that has the following structure - project -- logs -- project __init.py__ classA.py classB.py utils.py -- main.py I've se
Solution 1:
It is correct to configure logging only once for the whole project, as you first tried, not each package separately.
What you did wrong is you configured the logger for the current module only:
logger = logging.getLogger(__name__)
Instead of that, you want to configure the root logger:
root_logger = logging.getLogger()
root_logger.setLevel(logging.INFO)
# ...
root_logger.addHandler(logger_handler)
root_logger.addHandler(console_handler)
The configuration done for the root logger applies to every logger which does not explicitly override it.
Then you should use the specific logger when actually logging:
logger = logging.getLogger(__name__)
logger.warning("I am warning you about %s", something)
Post a Comment for "Python Logging - Multiple Modules"