Python 找不到记录器的处理程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44188270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
No handlers could be found for logger
提问by qwerty
I am newbie to python. I was trying logging in python and I came across No handlers could be found for loggererror while trying to print some warning through logger instance. Below is the code I tried
我是python的新手。我正在尝试登录 python,但在尝试通过记录器实例打印一些警告时,我遇到了找不到记录器错误的处理程序。下面是我试过的代码
import logging
logger=logging.getLogger('logger')
logger.warning('The system may break down')
And I get this error No handlers could be found for logger "logger"
并且我收到此错误No handlers could be found for logger "logger"
What's confusing me is when I first try to print warning using logging
and then through logger
, it works fine, like
让我感到困惑的是,当我第一次尝试使用logging
然后通过打印警告时logger
,它工作正常,例如
>>> import logging
>>> logging.warning('This is a WARNING!!!!')
WARNING:root:This is a WARNING!!!!
>>>
>>> logger.warning('WARNING!!!!')
WARNING:logger:WARNING!!!!
Can someone throw some light on what's happening in second scenario?
有人可以对第二种情况中发生的事情有所了解吗?
采纳答案by praba230890
For logging some message through logger
, in Python at least one handler should be added to the logger
object. By default the debug
, warn
and other functions in logging
module will call basicConfig
which in turn will add a StreamHandler
to the root logger
.
为了通过 记录一些消息logger
,在 Python 中至少应该向logger
对象添加一个处理程序。默认情况下debug
,模块中的,warn
和其他函数logging
将调用,而这些函数basicConfig
又会将 a 添加StreamHandler
到root logger
.
It's always recommendedto add your required Handler to your logger object you are writing for your module.
始终建议将所需的 Handler 添加到您为模块编写的记录器对象中。
You could refer to official Python docs, which has an awesome tutorialor you can better check out the source code of logging module yourself.
你可以参考官方的 Python 文档,它有一个很棒的教程,或者你可以更好地自己查看日志模块的源代码。
Simply you can check the source in Python shell itself by,
只需通过以下方式检查 Python shell 中的源代码,
import logging
import inspect
print(inspect.getsource(logging))
Finally, calling the basicConfig
explicitly will resolve the issue.
最后,basicConfig
显式调用将解决问题。
import logging
logging.basicConfig()
logger = logging.getLogger('logger')
logger.warning('The system may break down')
回答by phd
Call logging.basicConfig():
调用logging.basicConfig():
>>> import logging
>>> logging.basicConfig()
>>> logger = logging.getLogger('logger')
>>> logger.warning('The system may break down')
WARNING:logger:The system may break down
回答by user1767754
Additional to phd's answer, calling logging.basicConfig()
is a convenient function which will get you a default StreamHandler
and a Formatter
. That is enough if you want to quickly have a logging functionality. You can customize it's behaviour by passing basicConfig
some arguments:
除了 phd 的回答之外,调用logging.basicConfig()
是一个方便的功能,它可以为您提供默认值StreamHandler
和Formatter
. 如果您想快速拥有日志记录功能,这就足够了。您可以通过传递basicConfig
一些参数来自定义它的行为:
Add Useful parameters: output timestamp alongside the message
添加有用的参数:在消息旁边输出时间戳
logger = logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
This should be fine for most of the one off needs. If you need more control about your config, you can add more sophisticated behaviours by defining the logger's attributes your self.
这对于大多数一次性需求应该没问题。如果您需要更多地控制您的配置,您可以通过自己定义记录器的属性来添加更复杂的行为。
Sophisticated Example: without using the
basicConfig
function
复杂的例子:不使用
basicConfig
函数
import logging
logger = logging.getLogger("mylogger")
streamHandler = logging.StreamHandler()
streamHandler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
streamHandler.setFormatter(formatter)
logger.addHandler(streamHandler)
logger.info("Files copied")
logger.warning("disk quota exceeded")
>> 2017-12-06 11:11:12, 090 - mylogger - INFO Files copied
>> 2017-12-06 11:11:12, 091 - mylogger - WARNING disk quota exceeded
The next step in a bigger environment, would be deriving a new logger from the previously created one, to first keep the formatting and as well to maintain a "log hierarchy"
在更大的环境中,下一步是从之前创建的记录器派生一个新的记录器,首先保持格式并维护“日志层次结构”
logger2 = logging.getLogger("mylogger.new")
logger2.info("New Logger info")
>> 2017-12-06 11:11:12, 091 - mylogger.new - New logger info
A good reference is the logging cookbook:https://docs.python.org/2/howto/logging-cookbook.html
一个很好的参考是日志食谱:https : //docs.python.org/2/howto/logging-cookbook.html
回答by Ali
If you get this error when using sentry(No handlers could be found for logger "sentry.errors"
), it could be due to sentry bug for SNI support.
如果您在使用sentry( No handlers could be found for logger "sentry.errors"
)时遇到此错误,可能是由于 SNI 支持的 sentry 错误。
check https://github.com/getsentry/raven-python/issues/523for more details. a quick workaround is to replace DSN scheme by threaded+requests+https
:
查看https://github.com/getsentry/raven-python/issues/523了解更多详情。一个快速的解决方法是通过以下方式替换 DSN 方案threaded+requests+https
:
RAVEN_CONFIG = {
'dsn': 'threaded+requests+https://[email protected]/1',
}