Created
August 24, 2016 01:52
-
-
Save pisceanfoot/94a17745a3583c20d8e710cf90863224 to your computer and use it in GitHub Desktop.
a custom python logger handler
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 os | |
import logging | |
from logging.handlers import RotatingFileHandler | |
class RotatingFileHandlerPlus(RotatingFileHandler): | |
def __init__(self, filename, *arg, **kwarg): | |
logPath = os.environ.get('X_JOB_LOG_PATH') | |
if logPath: | |
filename = logPath | |
super(RotatingFileHandlerPlus, self).__init__(filename, *arg, **kwarg); | |
class ContextFormatter(logging.Formatter): | |
""" | |
This is a filter which injects contextual information into the log. | |
Rather than use actual contextual information, we just use random | |
data in this demo. | |
""" | |
def format(self, record): | |
# print record.getMessage() | |
s = super(ContextFormatter, self).format(record) | |
s = s.replace('\n', '\\n') | |
return s |
Author
pisceanfoot
commented
Aug 24, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment