Basic usage of Python log module logging

Table of contents

logging module

TimedRotatingFileHandler module 


logging module

When developing a large Python application, it may be necessary to record different levels of logging as well as debug information to quickly diagnose problems when they are encountered. Python's built-in logging module can help you with these tasks.

Python's logging module supports multiple log handlers, each of which can define its own log level. For example, you can write INFO-level logs to disk files, WARNING-level logs to the console, and ERROR-level logs to Email. This flexible configuration allows you to pass log information to different handlers according to your needs.

The logging module also supports formatted output of logs. You can specify the date format, log level, log information, and more. The output format of log information can be easily customized by using different formatting options.

The following are several important concepts in the logging module:

  • Logger: Logger is the main interface of the Python logging module, and you can create different loggers through Logger. You can create different Loggers for different modules or code blocks to implement log classification and module management.

  • Handler: Handler is a processor that outputs logs, which can write logs to files, consoles, or networks. Each Logger can have multiple Handlers, and you can configure different Handlers according to different requirements.

  • Formatter: Formatter defines the format of the log output, which can define the date format, log level, log information, etc.

  • Filter: Filter can be used to control which log records are processed. You can filter logs according to different filter conditions.

The usage process of the logging module is generally as follows:

  1. Create a Logger object, set the log level and log format for it;
  2. Create a Handler object, set the log level and log format for it;
  3. Add the Handler object to the Logger object;
  4. Use the Logger object to output logs.

Here is a simple example that demonstrates how to use the logging module to output logs to the console and to files:

import logging

# 创建Logger对象
logger = logging.getLogger('mylogger')
logger.setLevel(logging.DEBUG)

# 创建FileHandler对象
file_handler = logging.FileHandler('mylog.log')
file_handler.setLevel(logging.DEBUG)

# 创建StreamHandler对象
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.INFO)

# 创建Formatter对象
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
file_handler.setFormatter(formatter)
stream_handler.setFormatter(formatter)

# 添加Handler对象到Logger对象中
logger.addHandler(file_handler)
logger.addHandler(stream_handler)

# 输出日志
logger.debug('Debug message')
logger.info('Info message')
logger.warning('Warning message')
logger.error('Error message')
logger.critical('Critical message')

In this example, the Logger object is first created and the log level is set to DEBUG. Then create FileHandler and StreamHandler objects, which are used to output logs to files and consoles respectively. Then a Formatter object is created to define the format of the log output. Finally, FileHandler and StreamHandler are added to the Logger object, and different levels of log information are output through the Logger object.

By using the logging module, you can achieve very flexible logging and management. You can output logs to different places to implement log classification and module management. If you are developing a large-scale Python application, the logging module can help you better understand the running state of the application and help you quickly locate and solve problems.

TimedRotatingFileHandler module 

TimedRotatingFileHandlerIt is a Handler provided by the logging module in Python. Its function is to split log files according to a certain time interval. It automatically writes log content to new log files after certain time intervals for better management of log file size and number.

TimedRotatingFileHandlerThe constructor receives the following parameters:

  • filename: The name of the log file, including the path. If the file does not exist, it will be created automatically.
  • when: It is used to specify the time interval for splitting logs, and the values ​​are: S, M, H, D, W0-W6, and midnight. Among them, S, M, and H represent seconds, minutes, and hours respectively, D represents days, W0-W6 represents Sunday to Saturday, and midnight represents the early morning of every day.
  • interval: The time interval for splitting logs, the value is an integer, and the unit is whenthe specified time unit. For example: when whenit is 'D', interval1 means that the log is split every day, intervaland 7 means that the log is split every 7 days.
  • backupCount: The number of log files to keep, if this number is exceeded, the old log files will be deleted.

Here is an TimedRotatingFileHandlerexample of usage:

import logging.handlers

# 创建TimedRotatingFileHandler对象
handler = logging.handlers.TimedRotatingFileHandler(
    filename='mylog.log',
    when='D',
    interval=1,
    backupCount=7,
    encoding='utf-8'
)

# 创建Formatter对象
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)

# 创建Logger对象
logger = logging.getLogger('mylogger')
logger.setLevel(logging.DEBUG)
logger.addHandler(handler)

# 输出日志
logger.debug('Debug message')
logger.info('Info message')
logger.warning('Warning message')
logger.error('Error message')
logger.critical('Critical message')

In this example, an TimedRotatingFileHandlerobject is created that logs output to a file. Use whenspecifies to split log files by day, use to intervalspecify to split log files every day, and use backupCountto specify to keep 7 log files. Each time the program is run, the log output will be appended to mylog.logthe file, and if the file size exceeds the specified size, a new log file will be created automatically, and a maximum of 7 log files will be kept.

Guess you like

Origin blog.csdn.net/weixin_40582034/article/details/129134951
Recommended