Python Logging
01 Jul 2017Python`s logging module is very versatile and very helpful for debugging issues in Software.This article examines some of the basic concepts of logging.
Source Code Organization
There are 3 modules in the source code module1, module2, module3 . In main.py, we call functions from module1, module2. This would prove some of the concepts in logging module.
Concepts
The root logger is configured by using logging.basicConfig call. We can specify the format of a log message and also the logging level. This configuration is done only once in the code. It is mandated to call basicConfig() before you make the first call using the logger.
After the root logger is configured , all the loggers would use the same format and logging level by default.
When you want to debug a module you might want to increase the logging level for that module. This is done by configuring the logger of that module. This is applicable only to that module.
The main.py provides the main entry point for the project. It initializes the root logger. It also has a few log messages to demonstrate the logging at different levels. It calls a function f1 in module m1 and a function f2 from m2.
Program Execution and Output
The log messages indicate the time, severity, and also the origin of the message in terms of the source file and line number as follows