为什么INFO记录在DEBUG日志中 [英] Why does INFO get logged in DEBUG logs
问题描述
我正在使用yaml为我的Python应用程序配置日志记录.
I am using yaml to config logging for my Python application.
version: 1
disable_existing_loggers: False
formatters:
standard:
format: "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
handlers:
console:
class: logging.StreamHandler
level: DEBUG
formatter: standard
stream: ext://sys.stdout
info_file_handler:
class: logging.handlers.RotatingFileHandler
level: INFO
formatter: standard
filename: info.log
maxBytes: 10485760 # 10MB
backupCount: 20
encoding: utf8
error_file_handler:
class: logging.handlers.RotatingFileHandler
level: ERROR
formatter: standard
filename: errors.log
maxBytes: 10485760 # 10MB
backupCount: 20
encoding: utf8
debug_file_handler:
class: logging.handlers.RotatingFileHandler
level: DEBUG
formatter: standard
filename: debug.log
maxBytes: 10485760 # 10MB
backupCount: 20
encoding: utf8
loggers:
db_ops:
level: DEBUG
handlers: [info_file_handler, error_file_handler, debug_file_handler]
propagate: true
在模块 db_ops
中,我同时使用 logger.info
和 logger.debug
进行不同级别的日志记录.当我运行该应用程序时,虽然 INFO
确实输出到info.log,但 INFO
和 DEBUG
消息均输出到debug.log.
In module db_ops
, I use both logger.info
and logger.debug
for different levels of logging. When I run the app, while INFO
did get output to info.log, both INFO
and DEBUG
messages are output to debug.log.
根据级别将日志分隔到不同文件的正确方法是什么?
What is the right way to separate the log to different files based on the level?
推荐答案
python记录器和处理程序的级别是阈值.如果将级别指定为DEBUG,则表示将记录等于或大于DEBUG 的任何值.
The levels of python loggers and handlers are thresholds. If you specify your level as DEBUG, it means that anything equal or above DEBUG will be logged.
如果只想进行DEBUG日志记录,则必须另外分配一个过滤器,该过滤器将过滤除DEBUG消息以外的任何内容.
If you want to have a DEBUG logging only, you'll have to additionally assign a filter that will filter anything but DEBUG message.
这篇关于为什么INFO记录在DEBUG日志中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!