在我写的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("Some string")
时,我没有输出到控制台,即使this page in the docs说logging.debug
应该让根记录器输出消息。为什么我的程序没有输出任何内容,我该如何解决?
答案 0 :(得分:71)
默认日志记录级别为警告。 由于您没有更改级别,因此根记录器的级别仍然是警告。 这意味着它将忽略任何低于警告级别的日志记录,包括调试日志。
中对此进行了解释import logging
logging.warning('Watch out!') # will print a message to the console
logging.info('I told you so') # will not print anything
'info'行不会打印任何内容,因为级别高于info。
要更改级别,只需在根记录器中设置它:
'root':{'handlers':('console', 'file'), 'level':'DEBUG'}
换句话说,仅使用level = DEBUG定义处理程序是不够的,实际的日志记录级别也必须是DEBUG才能使其输出任何内容。
答案 1 :(得分:4)
也许试试看?看来在我的情况下删除了所有处理程序后,问题就解决了。
for handler in logging.root.handlers[:]:
logging.root.removeHandler(handler)
logging.basicConfig(filename='output.log', level=logging.INFO)
答案 2 :(得分:1)
很多年后,Python记录器似乎仍然存在可用性问题。以下是一些示例说明:
import logging
# This sets the root logger to write to stdout (your console)
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:
logging.basicConfig(level=logging.NOTSET)
# You can either share the `logger` object between all your files or the
# handle `my-app`. The result is the same.
logger = logging.getLogger("my-app")
logger.info("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.setLeve(logging.WARNING)
答案 3 :(得分:0)
对于这里任何想要超级简单答案的人:只需设置要显示的级别即可。我在所有脚本的顶部都放了:
Select id From tablename
然后显示该级别或更高级别的任何内容:
Select * From tablename Where id = clickedvalue
这是层次结构的一组级别,因此日志将以您设置的级别或更高显示。因此,如果要显示错误,可以使用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
。
这是一篇很好的文章,包含的信息比我的回答要好:
https://www.digitalocean.com/community/tutorials/how-to-use-logging-in-python-3
答案 4 :(得分:0)
import logging
log = logging.getLogger()
log.setLevel(logging.DEBUG)
此代码会将默认日志记录级别设置为 DEBUG。