Skip to content

Instantly share code, notes, and snippets.

@svartalf
Created December 11, 2013 06:23
Show Gist options
  • Save svartalf/7905857 to your computer and use it in GitHub Desktop.
Save svartalf/7905857 to your computer and use it in GitHub Desktop.
python log handler for fluent tcp transport
# -*- coding: utf-8 -*-
from logging.handlers import SocketHandler
import msgpack # https://pypi.python.org/pypi/msgpack-python
class FluentdHandler(SocketHandler):
def __init__(self, host='localhost', port=24224):
super(FluentdHandler, self).__init__(host, port)
self.packer = msgpack.Packer(encoding='utf-8')
def makePickle(self, record):
tag = '{0}.{1}'.format(record.name, record.funcName)
data = {
'level': record.levelname,
'message': record.msg % record.args,
'line': '{0}.{1}:{2}'.format(record.name, record.funcName, record.lineno),
}
return self.packer.pack([tag, record.created, data])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment