Home  >  Article  >  Backend Development  >  The secret recipe of Python’s logging module: building an efficient logging system

The secret recipe of Python’s logging module: building an efficient logging system

王林
王林forward
2024-03-08 08:40:03787browse

Python logging 模块的秘密配方:打造高效日志系统

python The logging module is a powerful and versatile tool that can be used for logging in Python applications and admin log messages. By mastering the secret recipe of the logging module, you can create an efficient and easy-to-maintain logging system that improves the reliability and debuggability of your application.

Custom log level

The logging module provides a set of predefined log levels, including DEBUG, INFO, WARNING, ERROR, and CRITICAL. You can customize these levels to create a more granular logging hierarchy based on your application's needs. For example, you can add a custom level, such as "TRACE", to log detailed events that occur in your application.

import logging

# 创建自定义日志级别
TRACE = logging.DEBUG - 5
logging.addLevelName(TRACE, "TRACE")

# 创建一个使用自定义级别的日志记录器
logger = logging.getLogger(__name__)
logger.setLevel(TRACE)

Use multiple log handlers

The log handler is responsible for sending log messages to different destinations, such as a file, the console, or a remote server . The logging module provides a range of built-in handlers, and you can create custom handlers to meet your specific needs. Using multiple handlers, you can log messages to multiple targets simultaneously, providing more comprehensive logging.

# 创建一个文件处理程序
file_handler = logging.FileHandler("my_log.log")

# 创建一个控制台处理程序
console_handler = logging.StreamHandler()

# 为日志记录器添加处理程序
logger.addHandler(file_handler)
logger.addHandler(console_handler)

Log format

The log format specifies the structure and layout of the log message. The logging module provides a flexible formatting system that allows you to customize the appearance of log messages. Using the log format, you can include information such as message content, timestamp, log level, and calling code source.

# 创建一个自定义日志格式
fORMatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")

# 将格式应用于处理程序
file_handler.setFormatter(formatter)
console_handler.setFormatter(formatter)

Filter log messages

The logging module allows you to filter log messages and only log messages that match certain criteria. Filters can be created based on log level, message content, or other custom criteria. Filtering log messages can reduce log output and ensure that only messages useful for debugging or analysis are logged.

# 创建一个过滤器以只记录 ERROR 和 CRITICAL 消息
filter = logging.Filter()
filter.filter = lambda record: record.levelno >= logging.ERROR

# 将过滤器应用于一个处理程序
file_handler.addFilter(filter)

Log propagation

Log propagation controls how log messages are propagated. By default, log messages are propagated to all loggers in the application that contain handlers. However, you can control the flow of messages through the logger hierarchy by configuring log propagation. This helps prevent duplicate and redundant log messages.

# 禁用日志传播
logger.propagate = False

Log rotation

Log rotation limits the maximum size of a single log file, preventing the file from becoming too large to manage. The logging module provides a FileHandler class that supports automatic log rotation and creates new log files when the log file reaches a specified size or time limit.

# 创建一个带日志旋转的文件处理程序
file_handler = logging.handlers.RotatingFileHandler("my_log.log", maxBytes=1024, backupCount=5)

Performance optimization

The logging module incurs some overhead when recording log messages. To optimize performance, you can use the following tips:

  • Only log information useful for debugging or analysis.
  • Use filters to reduce log output.
  • Use efficient log format.
  • Avoid frequent creation and destruction of loggers.
  • Use an asynchronous logger to reduce main thread blocking.

By mastering these secret recipes of the Python logging module, you can create powerful logging systems and improve the reliability and debuggability of your applications.

The above is the detailed content of The secret recipe of Python’s logging module: building an efficient logging system. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lsjlt.com. If there is any infringement, please contact admin@php.cn delete