python-logstash icon indicating copy to clipboard operation
python-logstash copied to clipboard

Feature Request > option to add extra fields to records globally

Open croepha opened this issue 9 years ago • 3 comments

I find my self doing this:

class CustomUDPLogstashHandler(logstash.UDPLogstashHandler):
    def __init__(self, *a, **kw):
        self.global_extras = kw.pop('global_extras', {})
        super().__init__(*a, **kw)

    def emit(self, record):
        for key, value in self.global_extras.items():
            setattr(record, key, value)
        super().emit(record)

LOGGING={
...
        'logstash': {
            'level': 'DEBUG',
            'class': 'util.CustomUDPLogstashHandler',
            'host': 'logstash.service',
            'port': 5000,
            'version': 1,
            'tags': ['tag1', 'tag2'],
            'global_extras': {
                'product': "my_test_product",
                'instance': "my_test_instance",
                'site': 333
            }
        }
...

It would be cool if that functionality were included

croepha avatar Aug 26 '15 14:08 croepha

:+1:

sumdog avatar Mar 08 '16 23:03 sumdog

It's implemented (but not merged yet) in #42

mkurek avatar Jul 21 '17 05:07 mkurek

Hi ! It would be great to have this feature.

Being available to add extra fields globally just in one place in all the loggers defined in our code would be so helpful. So we can have those extra fields as keys in the json sent by this logstash handler.

Currently, we would need to create subclasses of LoggerAdapter for each logger defined to be adding all our needed extra fields in our logs. Furthermore, there will also be loggers that we cannot modify from other libraries.

mrodm avatar Apr 06 '20 21:04 mrodm