Last active
August 29, 2015 14:02
-
-
Save drj11/e8e81ef14bc33dcac888 to your computer and use it in GitHub Desktop.
logging usercustomize.py
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
#!/usr/bin/env python | |
import logging | |
import logging.handlers | |
import os | |
import socket | |
import sys | |
class UnpickledSocketHandler(logging.handlers.SocketHandler): | |
""" | |
This handler sends an unformatted string (newline | |
terminated) to the socket. In other words it avoids the | |
pickling that SockerHandler does. | |
""" | |
def makePickle(self, record): | |
formatted = self.format(record) | |
return formatted + "\n" | |
class JSONSocketHandler(logging.handlers.SocketHandler): | |
""" | |
This handler sends a newline terminated JSON object to the | |
socket. | |
""" | |
def makePickle(self, record): | |
j = json.dumps(record.__dict__) | |
return j + '\n' | |
handler = UnpickledSocketHandler('log.scraperwiki.com', 1515) | |
logging_format = os.environ.get('USER', "$USER-not-set") + " %(levelname)s python %(name)s %(message)s" | |
formatter = logging.Formatter(logging_format, None) | |
handler.setFormatter(formatter) | |
logging.root.addHandler(handler) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment