Home >Backend Development >Python Tutorial >10 Tips for Mastering the Python Logging Module
In addition to the default DEBUG, INFO, WARNING, ERROR and CRITICAL levels, you can create custom levels. This is useful for differentiating events of varying severity.
import logging # 创建自定义日志级别 CUSTOM_LEVEL = logging.INFO + 5 logging.addLevelName(CUSTOM_LEVEL, "CUSTOM") # 创建一个 Logger 并设置自定义日志级别 logger = logging.getLogger("my_logger") logger.setLevel(CUSTOM_LEVEL)
The processor is responsible for sending log events to a specific destination, such as a file or console. You can customize the processor to meet your specific needs.
import logging # 创建一个 FileHandler 并设置日志文件名 file_handler = logging.FileHandler("my_log.txt") # 创建一个 StreamHandler 并输出到控制台 stream_handler = logging.StreamHandler() # 将处理器添加到 Logger logger = logging.getLogger("my_logger") logger.addHandler(file_handler) logger.addHandler(stream_handler)
Filters allow you to filter log events based on specific criteria. This is useful for logging only events of interest.
import logging # 创建一个过滤器以过滤 INFO 级别以上的事件 info_filter = logging.Filter() info_filter.filter = lambda record: record.levelno >= logging.INFO # 将过滤器添加到 Logger logger = logging.getLogger("my_logger") logger.addFilter(info_filter)
You can customize the format of log events to provide the required information.
import logging # 创建一个 FORMatter 并设置格式字符串 formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s") # 将 Formatter 添加到处理器 handler = logging.StreamHandler() handler.setFormatter(formatter) # 将处理器添加到 Logger logger = logging.getLogger("my_logger") logger.addHandler(handler)
Context processors allow you to add additional information when logging. This is useful for tracking context within a request or transaction.
import logging from contextlib import contextmanager # 创建一个上下文处理器以添加请求 ID @contextmanager def request_id_context(request_id): previous_request_id = logging.currentframe().f_locals.get("request_id") try: logging.currentframe().f_locals["request_id"] = request_id yield finally: logging.currentframe().f_locals["request_id"] = previous_request_id # 使用上下文处理器 logger = logging.getLogger("my_logger") with request_id_context("1234"): logger.info("Received request")
You can easily configure the Logging module using a dictionary.
import logging # 配置字典 logging_config = { "version": 1, "formatters": { "default": { "format": "%(asctime)s - %(levelname)s - %(message)s" } }, "handlers": { "file": { "class": "logging.FileHandler", "filename": "my_log.txt", "formatter": "default", }, "console": { "class": "logging.StreamHandler", "formatter": "default", } }, "loggers": { "my_logger": { "handlers": ["file", "console"], "level": "INFO", } } } # 从字典配置 Logging logging.config.dictConfig(logging_config)
The Logging module can be integrated with third-party packages such as Sentry or Rollbar. This allows you to easily send log events to a remote service.
import logging import sentry_sdk # 初始化 Sentry 并与 Logging 集成 sentry_sdk.init() logging.basicConfig(level=logging.INFO, handlers=[sentry_sdk.handler.SentryHandler()])
Logging module supports multi-threaded applications. It uses thread local storage to ensure that each thread has its own independent log processor.
import logging import threading # 创建线程安全的 Logger logger = logging.getLogger("my_logger") # 创建一个线程并向 Logger 记录 def thread_function(): logger.info("Executing in a separate thread") # 启动线程 thread = threading.Thread(target=thread_function) thread.start()
Logging module can automatically record exceptions that occur.
import logging # 创建一个 Logger logger = logging.getLogger("my_logger") # 记录一个异常 try: raise Exception("An error occurred") except Exception as e: logger.exception(e)
python 3.8 introduces support for extended logging. This allows you to create custom logging functions and handlers.
import logging # 创建一个自定义日志记录函数 def my_log_function(logger, level, msg, *args, **kwargs): # 您的自定义日志记录逻辑 # 添加自定义日志记录函数到 Logger logger = logging.getLogger("my_logger") logger.addHandler(logging.NullHandler()) logger.addFilter(logging.Filter()) logger.log = my_log_function
The above is the detailed content of 10 Tips for Mastering the Python Logging Module. For more information, please follow other related articles on the PHP Chinese website!