Skip to content

Instantly share code, notes, and snippets.

@jsh2134
Created July 12, 2015 21:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsh2134/14027c69464498bc8ed2 to your computer and use it in GitHub Desktop.
Save jsh2134/14027c69464498bc8ed2 to your computer and use it in GitHub Desktop.
HekaSockerHandler logging implementation
import traceback
import logging
from logging.handlers import SocketHandler
import heka
class HekaSocketHandler(SocketHandler):
SEVERITY_MAP = {
logging.CRITICAL: heka.CRITICAL,
logging.ERROR: heka.ERROR,
logging.WARNING: heka.WARNING,
logging.INFO: heka.INFO,
logging.DEBUG: heka.DEBUG,
logging.NOTSET: heka.INFO,
}
def emit(self, record):
"""
Emit a record.
Uses heka-py library to convert the record to ProtoBuf message
and then writes it to the socket in binary format.
If there is an error with the socket, silently drop the packet.
If there was a problem with the socket, re-establishes the
socket.
"""
try:
s = self.to_heka_protobuf_message(record)
self.send(s)
except (KeyboardInterrupt, SystemExit):
raise
except:
self.handleError(record)
def to_heka_protobuf_message(self, record):
tb = ''
message = ''
if record.exc_info:
tb = traceback.format_exc(record.exc_info[2])
if hasattr(record, 'message'):
message = record.message
msg = heka.Message(
type='python',
severity=self.SEVERITY_MAP.get(record.levelno, heka.INFO),
payload=message,
fields={'name': record.name,
'message': message,
'traceback': tb
},
)
return heka.frame(msg.encode())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment