A Guide to Logging for Python Developers: For Better Debugging

Posted by

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 NameformatDescription
asctime%(asctime)sHuman-readable time
lineno%(lineno)dLine number where the logging call was made
name%(name)sLogger’s name
levelname%(levelname)sLog level
message%(message)sThe log message
created%(created)fThe time the log was created
filename%(filename)sThe name of the file in the path name
funcName%(funcName)sThe name of the function including the logging call
module%(module)sThe module name of the file name
pathname%(pathname)sThe full path name
thread%(thread)dThread ID
threadName%(threadName)sThread name
format attribute

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.

Leave a Reply

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다