I did some interactive testing and it looks like app.logger
is a logger named with python file
(我进行了一些交互式测试,看来app.logger
是一个以python文件命名的记录器)
print(app.logger.name) # filename
and it has one handler
(它有一个处理程序)
print(app.logger.handlers) # [<StreamHandler <stderr> (NOTSET)>]
A handler with level logging.NOTSET
processes all messages (from all logging levels).
(具有级别日志记录的处理程序logging.NOTSET
处理所有消息(来自所有日志记录级别)。)
So when you set app.logger.setLevel(logging.DEBUG)
then all debug and higher logs will be passed to handlers and all of them will appear on stderr. (因此,当您设置app.logger.setLevel(logging.DEBUG)
所有调试和更高级别的日志都将传递给处理程序,并且所有它们都将出现在stderr上。)
To log absolutely nothing on the screen you have to manually remove StreamHandler:
(要在屏幕上完全不记录任何内容,您必须手动删除StreamHandler:)
app.logger.handlers.pop(0)
and to log DEBUG and higher to the file set logging level also on the handler
(并在处理程序上将DEBUG和更高级别记录到文件集日志记录级别)
log_handler.setLevel(logging.DEBUG)
Python logging is quite complicated.
(Python日志记录非常复杂。)
See this logging flow chart for better understanding what is going on :) (请参阅此日志记录流程图以更好地了解正在发生的事情:))
EDIT: to log only one specific level you need custom Filter object:
(编辑:仅记录一个特定级别,您需要自定义过滤器对象:)
class LevelFilter:
def __init__(self, level):
self._level = level
def filter(self, log_record):
return log_record.levelno == self._level
log_handler.setLevel(logging.DEBUG)
log_handler.addFilter(LevelFilter(logging.DEBUG))
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…