python-logstash
python-logstash copied to clipboard
Added ``extra_fields`` parameter to handler
extra_fields allow to send a additional parameters to logstash in key value format.
@quamilek, have you considered to utilize python LoggerAdapter (https://docs.python.org/2/library/logging.html#loggeradapter-objects) for this purpose?
@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?
Hello! It was a while ago, but any chance this can be merged ?
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.