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

Added ``extra_fields`` parameter to handler

Open quamilek opened this issue 9 years ago • 4 comments
trafficstars

extra_fields allow to send a additional parameters to logstash in key value format.

quamilek avatar Nov 27 '15 08:11 quamilek

@quamilek, have you considered to utilize python LoggerAdapter (https://docs.python.org/2/library/logging.html#loggeradapter-objects) for this purpose?

vklochan avatar Aug 17 '16 13:08 vklochan

@vklochan do you have an example how to use LoggerAdapter painlessly (especially in Django) - I don't want to add adapter to every logger instance I create? Is there a way to apply it globally?

mkurek avatar Jul 21 '17 05:07 mkurek

Hello! It was a while ago, but any chance this can be merged ?

danil-topchiy avatar Jul 09 '21 13:07 danil-topchiy

In theory, you can achieve the functionality with logging.setLoggerClass.

However this solution is somewhat inelegant. Here we pass an extra field application:


            logger = logging.getLogger()
            handler = logstash.UDPLogstashHandler(logstash_server, 5959, version=1, tags=tags)

            # Add handler to the root logger
            logger.addHandler(handler)

            class VeryCustomLogger(logging.Logger):
                """A logger that allows you to register adapters on a instance."""

                def __init__(self, name):
                    """Create a new logger instance."""
                    super().__init__(name)

                def _log(self, level, msg, *args, **kwargs):
                    """Let adapters modify the message and keyword arguments."""
                    kwargs["application"] = application_name
                    return super()._log(level, msg, *args, **kwargs)

            # All future logging.getLogger() instances will use this VeryCustomLogger configuration
            logging.setLoggerClass(VeryCustomLogger)

It is cleaner to pass extra fields to the LogStash handler directly. Generally, these fields are LogStash specific and might not bore context in file/console logging, so LogStash handler feels like a right place for them.

miohtama avatar Feb 16 '22 09:02 miohtama