[英]Python logging - overview of currently installed loggers/handlers
I am trying to set up a Fluentd log handler from a Django project using a logger from code: 我正在尝试使用代码记录器从Django项目中设置Fluentd日志处理程序:
def get_fluentd_logger(name):
import logging
from fluent import handler
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(name)
logger.addHandler(handler.FluentHandler(name, host='localhost', port=24224))
return logger
handler.FluentHandler
comes from package fluent-logger
and I am running fluent
locally. handler.FluentHandler
来自fluent-logger
软件包,我在本地运行fluent
。
fluent.conf: fluent.conf:
<source>
type forward
port 24224
</source>
<match **>
type copy
<store>
type stdout
</store>
<store>
type forward
send_timeout 60s
recover_wait 10s
heartbeat_interval 1s
phi_threshold 8
hard_timeout 60s
<server>
name monitoring
host 1.2.3.4
port 24224
weight 100
</server>
</store>
</match>
When I run this from a non-django python project it works fine, but when called from django it just does not do anything. 当我从非django python项目运行此程序时,它工作正常,但从django调用时,它什么也没做。
The question is: is there a way to see the currently installed loggers and their handlers so I can debug this situation? 问题是:是否可以查看当前安装的记录器及其处理程序,以便我可以调试这种情况?
[EDIT] [编辑]
When done from the django settings like this: 从Django设置完成后,如下所示:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'simple': {
'format': '%(levelname)s: %(message)s'
},
},
'handlers': {
'fluentdebug':{
'level':'DEBUG',
'class':'fluent.handler.FluentHandler',
'formatter': 'simple',
'tag':'foo.bar.baz',
'host':'localhost',
'port':24224,
},
},
'loggers': {
'foo.bar.baz': {
'handlers': ['fluentdebug'],
'level': 'DEBUG',
'propagate': True,
},
}
}
It does work. 确实有效。 I however would like to be able to do this from code because foo.bar.baz
can take many values and I dont want to pollute this file with 20 loggers and handlers that do exactly the same. 但是,我希望能够通过代码执行此操作,因为foo.bar.baz
可以采用许多值,并且我不想使用20个完全相同的记录器和处理程序来污染此文件。
Maybe the real question is: Why cant I add loggers from code to logging
after Django has performed it's setup? 也许真正的问题是:为什么在Django执行设置之后,为什么不能从代码添加记录器到logging
?
This isn't something I've dealt with before, but Logging Tree sounds like what you might be looking for. 这不是我以前处理过的事情,但是Logging Tree听起来像您想要的东西。 You ought to read Brandon's post about it, but here's an example of output: 您应该阅读Brandon的相关文章 ,但这是输出示例:
<--""
Level WARNING
Handler Stream <open file '<stderr>', mode 'w' at ...>
|
o<--[cherrypy]
|
o<--"cherrypy.access"
| Level INFO
| Handler Stream <open file '<stdout>', mode 'w' ...>
|
o<--"cherrypy.error"
Level INFO
Handler Stream <open file '<stderr>', mode 'w' ...>
I am going to answer myself because if somebody runs in the same problem it is easier to find. 我要回答自己,因为如果有人遇到相同的问题,就更容易找到。 The problem seems to be with the FluentHandler
. 问题似乎出在FluentHandler
。 Using Brandon's Logging tree as mentioned by @Thomas we could see that the loggers were correctly added. 使用@Thomas提到的Brandon的Logging树,我们可以看到记录器已正确添加。
However further debugging showed that if you not explicitly set the loglevel it is set to NotSet
. 但是,进一步的调试显示,如果未显式设置NotSet
级别,则将其设置为NotSet
。 Somehow in "ordinary" python this is ok and the record is emitted. 在“普通” python中以某种方式可以这样做,并发出记录。 In Django however NotSet
level logs are thrown away. 但是在Django中, NotSet
级别的日志将被丢弃。 I don't know if this is standard Django behaviour, but it caused a lot of headaces. 我不知道这是否是标准的Django行为,但这引起了很多麻烦。 Below is the code that worked: 以下是有效的代码:
def get_fluentd_logger(name):
import logging
from fluent import handler
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(name)
logger.level = logging.INFO
handler = handler.FluentHandler(name, host='localhost', port=24224)
handler.level = logging.INFO
logger.addHandler(handler)
return logger
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.