Python日志没有输出任何内容
在我写的一个Python脚本中,我想用日志模块来记录事件。我有以下代码来配置我的日志记录器:
ERROR_FORMAT = "%(levelname)s at %(asctime)s in %(funcName)s in %(filename) at line %(lineno)d: %(message)s"
DEBUG_FORMAT = "%(lineno)d in %(filename)s at %(asctime)s: %(message)s"
LOG_CONFIG = {'version':1,
'formatters':{'error':{'format':ERROR_FORMAT},
'debug':{'format':DEBUG_FORMAT}},
'handlers':{'console':{'class':'logging.StreamHandler',
'formatter':'debug',
'level':logging.DEBUG},
'file':{'class':'logging.FileHandler',
'filename':'/usr/local/logs/DatabaseUpdate.log',
'formatter':'error',
'level':logging.ERROR}},
'root':{'handlers':('console', 'file')}}
logging.config.dictConfig(LOG_CONFIG)
但是当我尝试运行 logging.debug("一些字符串")
时,控制台没有任何输出。尽管文档中的这一页说 logging.debug
应该让根日志记录器输出这个信息。为什么我的程序没有输出任何内容,我该怎么解决呢?
10 个回答
对于想要简单答案的朋友们:只需要设置你想要显示的日志级别。在我所有的脚本开头,我通常会写:
import logging
logging.basicConfig(level = logging.INFO)
然后要显示该级别或更高级别的任何内容:
logging.info("Hi you just set your fleeb to level plumbus")
这里有五个级别的日志,按照严重程度从低到高排列,日志会显示你设置的级别或更高的级别。所以如果你想显示一个错误信息,可以用 logging.error("The plumbus is broken")
。
这些级别从低到高分别是 DEBUG
、INFO
、WARNING
、ERROR
和 CRITICAL
。默认的设置是 WARNING
。
这是一篇很好的文章,里面的信息表达得比我更清楚:
https://www.digitalocean.com/community/tutorials/how-to-use-logging-in-python-3
许多年后,Python的日志记录器似乎仍然存在一些使用上的问题。下面是一些解释和例子:
import logging
# This sets the root logger to write to stdout (your console).
# Your script/app needs to call this somewhere at least once.
logging.basicConfig()
# By default the root logger is set to WARNING and all loggers you define
# inherit that value. Here we set the root logger to NOTSET. This logging
# level is automatically inherited by all existing and new sub-loggers
# that do not set a less verbose level.
logging.root.setLevel(logging.NOTSET)
# The following line sets the root logger level as well.
# It's equivalent to both previous statements combined:
logging.basicConfig(level=logging.NOTSET)
# You can either share the `logger` object between all your files or the
# name handle (here `my-app`) and call `logging.getLogger` with it.
# The result is the same.
handle = "my-app"
logger1 = logging.getLogger(handle)
logger2 = logging.getLogger(handle)
# logger1 and logger2 point to the same object:
# (logger1 is logger2) == True
logger = logging.getLogger("my-app")
# Convenient methods in order of verbosity from highest to lowest
logger.debug("this will get printed")
logger.info("this will get printed")
logger.warning("this will get printed")
logger.error("this will get printed")
logger.critical("this will get printed")
# In large applications where you would like more control over the logging,
# create sub-loggers from your main application logger.
component_logger = logger.getChild("component-a")
component_logger.info("this will get printed with the prefix `my-app.component-a`")
# If you wish to control the logging levels, you can set the level anywhere
# in the hierarchy:
#
# - root
# - my-app
# - component-a
#
# Example for development:
logger.setLevel(logging.DEBUG)
# If that prints too much, enable debug printing only for your component:
component_logger.setLevel(logging.DEBUG)
# For production you rather want:
logger.setLevel(logging.WARNING)
一个常见的困惑来源于根日志记录器初始化得不好。看看这个例子:
import logging
log = logging.getLogger("myapp")
log.warning("woot")
logging.basicConfig()
log.warning("woot")
输出:
woot
WARNING:myapp:woot
根据你的运行环境和日志级别,第一条日志(在基本配置之前)可能根本不会显示出来。
默认的日志记录级别是“警告”。
因为你没有改变这个级别,所以根日志记录器的级别仍然是“警告”。这意味着它会忽略任何低于“警告”级别的日志,包括“调试”日志。
这个内容在教程中有解释:
import logging
logging.warning('Watch out!') # will print a message to the console
logging.info('I told you so') # will not print anything
因为“信息”级别的日志低于“警告”,所以它不会打印任何内容。
要改变这个级别,只需要在根日志记录器中设置:
'root':{'handlers':('console', 'file'), 'level':'DEBUG'}
换句话说,仅仅定义一个级别为“调试”的处理器是不够的,实际的日志记录级别也必须设置为“调试”,这样才能输出任何内容。