graylog filters is not setup for django after configuration

697 views Asked by At

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.

1

There are 1 answers

0
RandomEli On BEST ANSWER

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 from logging.Filter before dictionary configuration.

class FieldFilter(logging.Filter):
    def __init__(self, fields):
        self.fields = fields

    def filter(self, record):
        for k, v in self.fields.items():
            setattr(record, k, v)
        return True

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. Similar dictConfig could be easily applied in pygelf.