While developing in Python, you often find yourself checking various things through print
statements. For debugging, constantly printing outputs and then having to comment out or delete all print
statements before the final release can be cumbersome. Moreover, if you need to debug again later, the hassle of rewriting print
statements arises.
This is where logging
becomes handy.
Logging allows you to create logs during program execution and set the level of information to be output to the stream. This way, you can output information related to normal operations under regular circumstances, and output detailed items for debugging when necessary.
Using logging
Creating a logger
If a name is not set, a root logger is created. It’s better to assign names according to the function and purpose.
import logging logger = logging.getLogger(__name__)
Setting the logger level
The level sets the criterion for output. Logs below the set level will not be output. Levels can be set by name or by numeric value. The order of levels is: CRITICAL > ERROR > WARNING > INFO > DEBUG
Descriptions by level (Numeric Value)
CRITICAL(50): Indicates that the software is inoperable at a critical error level. ERROR(40): Indicates that the software cannot perform some functions due to a serious problem. WARNING(30): Indicates that, although the software is operating, something unexpected has happened or is predicted to happen. INFO(20): Confirmation messages indicating that operations are proceeding as planned. DEBUG(10): Records information needed to diagnose problems simply.
Therefore, it’s common to use the INFO or WARNING levels regularly, and set the output level to DEBUG during development.
logger.setLevel(logging.INFO)
Setting the formatter
The formatter determines how messages are output, allowing them to be set in a user-friendly manner.
format = "[%(asctime)-10s] (Line No: %(lineno)d) %(name)s:%(levelname)s - %(message)s"
Attribute Name | format | Description |
asctime | %(asctime)s | Human-readable time |
lineno | %(lineno)d | Line number where the logging call was made |
name | %(name)s | Logger’s name |
levelname | %(levelname)s | Log level |
message | %(message)s | The log message |
created | %(created)f | The time the log was created |
filename | %(filename)s | The name of the file in the path name |
funcName | %(funcName)s | The name of the function including the logging call |
module | %(module)s | The module name of the file name |
pathname | %(pathname)s | The full path name |
thread | %(thread)d | Thread ID |
threadName | %(threadName)s | Thread name |
Final code
import logging logger = logging.getLogger('DeepLink_Blog') format = '[%(asctime)-10s] (Line No: %(lineno)d) %(name)s:%(levelname)s - %(message)s' logging.basicConfig(format=format) logger.setLevel(logging.INFO) logger.info('Well Done')
[2024-04-08 09:13:51,978] (Line No: 7) DeepLink_Blog:INFO - Well Done
By leveraging the logging
module instead of print
statements, Python developers can create a more professional and efficient debugging environment. This guide provides the essentials to get started with logging, making your debugging process smoother and more manageable.