Python logging formatter for creating log entries in a JSON logstash-friendly format.
MIT License
Python logging formatter for creating log entries in a JSON logstash-friendly format.
Supports renaming of python default logging fields to logstash friendly names (e.g: renaming asctime to @timestamp)
LogstashFormatter can receive the following arguments:
.. code:: python
{
"@timestamp": "2016-09-28T16:24:24,799",
"@version": "1",
"exc_info": null,
"filename": "<ipython-input-21-de248ad5b09c>",
"funcName": "<module>",
"levelname": "INFO",
"message": "This is a normal message to be logged"
}
Add LogstashFormatter as the formatter of your handler, as usual:
.. code:: python
self.logger = logging.getLogger()
self.handler = logging.StreamHandler()
self.handler.setFormatter(LogstashFormatter())
self.logger.addHandler(self.handler)
Include logstash formatter in your settings file:
.. code:: python
LOGGING = {
'formatters': {
'logstash': {
'()': 'logstash_formatter.LogstashFormatter',
'format': ("asctime", "levelname", "name", "lineno", "message",
"pathname", "module", "funcName", "process",),
'datefmt': '%Y-%m-%dT%H:%M:%S',
'rename': {
'asctime': '@timestamp',
},
'version': '1'
},
}
}