I've set up a logging configuration as the dict below:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'simple': {
'format': '%(levelname)s %(message)s'
}
},
'filters': {
'fields': {
'env': 'test'
}
},
'handlers': {
'graypy': {
'level': 'DEBUG',
'class': 'graypy.GELFHandler',
'host': 'graylog2.example.org',
'port': 12201,
'filters': ['fields']
}
},
'loggers': {
'testlogger': {
'handlers': ['graypy'],
'level': 'DEBUG',
'propagate': True
}
}
}
After I run the application phase through manage.py
, I didn't see the filters
, env:test
popped up for me on graylog GUI, so I checked the settings locally with python manage.py shell
. In the console, I have the following check:
l = logging.getLogger('testlogger')
l.handlers[0].filters[0].fields
And it returns AttributeError: 'Filter' object has no attribute 'fields'
, which expectedly should have env:test
in the list. I've read a couple of tutorials of how to setup filters
for GELFhandler
in django, it seems like they all have similar format like my settings above, I don't know why only filters are not set for my logger.
I figured this out. For some reason,
handlers
could be configured directly by defining attributes in dictionary. However, for filters I need to add a childclass fromlogging.Filter
before dictionary configuration.And then I'm able to see the env is set as one filter.
UPDATE:
It's not a consistent change to make in
settings.py
, for long term perspective, you don't want to do this for your django project. SimilardictConfig
could be easily applied inpygelf
.