Created
July 12, 2015 21:24
-
-
Save jsh2134/14027c69464498bc8ed2 to your computer and use it in GitHub Desktop.
HekaSockerHandler logging implementation
This file contains hidden or 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 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