vklochan/python-logstash

Feature Request > option to add extra fields to records globally

croepha opened this issue · 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

👍

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

mrodm commented

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.