Last active
July 11, 2017 11:52
-
-
Save johnwheeler/0fce845f49061265854d to your computer and use it in GitHub Desktop.
Python Logstash Formatter compatible with json codec format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
import json | |
class LogstashFormatter(logging.Formatter): | |
def __init__(self, fmt=None, datefmt='%Y-%m-%dT%H:%M:%SZ'): | |
super(LogstashFormatter, self).__init__(fmt, datefmt) | |
def format(self, record): | |
obj = record.__dict__.copy() | |
obj['@timestamp'] = self.formatTime(record, self.datefmt) | |
obj['message'] = super(LogstashFormatter, self).format(record) | |
obj.pop('msg') | |
return json.dumps(obj) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My take on a LogstashFormatter for python. Use it like: