Python Logging - Overview of Currently Installed Loggers / Handlers

I am trying to configure the Fluentd log handler from a Django project using the logger from the code:

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 the fluent-logger package, and I run fluent locally.

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 nothing.

The question is: is there a way to see the currently installed registrars and their handlers so that I can debug this situation?

[EDIT]

When you do the django settings as follows:

 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, }, } } 

He works. I would like to be able to do this from code, because foo.bar.baz can take many values, and I do not want to pollute this file with 20 registrars and handlers that do the same.

Perhaps the real question is: why can't I add loggers from code to logging after Django has configured it?

+4
source share
2 answers

I am going to answer myself, because if someone works on the same problem, it is easier to find. The problem seems to be related to FluentHandler . Using the Brandon logging tree, as @Thomas mentioned, we could see that the loggers were correctly added.

However, further debugging showed that if you did not explicitly install loglevel, it is installed on NotSet . Somehow in the "regular" python, this is normal, and the record is emitted. However, Django NotSet discards level logs. I do not know if this is the standard behavior of Django, but it caused a lot of goals. 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 
+2
source

This is not what I talked about before, but the Log Tree sounds like you're looking. You should read Brandon 's post about this, but here is an example output:

 <--"" 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' ...> 
+3
source

Source: https://habr.com/ru/post/1446421/


All Articles