severb / graypy

Python logging handler for Graylog that sends messages in GELF (Graylog Extended Log Format).
https://www.graylog.org/
BSD 3-Clause "New" or "Revised" License
260 stars 91 forks source link

Problem adding filters for django logging #79

Open LingboTang opened 7 years ago

LingboTang commented 7 years ago

I have the following settings for django in settings.py:

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

But I didn't see any filters setup for graylog in GUI, so I checked it locally with logging.getLogger('testlogger').handlers[0].filters[0].__dict__ and it returns {'name': '', 'nlen': 0}. Then I made a little change that I throw the filters out handlers and put that in loggers like:

'loggers': {
            'testlogger': {
                'handlers': ['graypy'],
                'filters': ['fields'],
                'level': 'DEBUG',
                'propagate': True
            }
        }

And it didn't work as well. The only way seems working for me is manually add a filter class just like the sample in README. So I don't know if dict config hasn't been supported by graylog or I did something in my config.

LingboTang commented 7 years ago

If I add filters through a subclass that is inherited from logging.Filter, I should be able to log some message to graylog through manage.py shell with self-defined filters. However, I still couldn't get any messages log to graylog when I run my django server and sending resquest to django after I defined the loggers.