Skip to content

Instantly share code, notes, and snippets.

@pisceanfoot
Created August 24, 2016 01:52
Show Gist options
  • Save pisceanfoot/94a17745a3583c20d8e710cf90863224 to your computer and use it in GitHub Desktop.
Save pisceanfoot/94a17745a3583c20d8e710cf90863224 to your computer and use it in GitHub Desktop.
a custom python logger handler
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
@pisceanfoot
Copy link
Author

[handler_fileHandler]
class=messageSub.lib.loghandler.RotatingFileHandlerPlus
level=DEBUG
formatter=simpleFormatter
args=('log.log','a',2000000,5,)

[formatter_simpleFormatter]
format=[%(asctime)s] [%(levelname)s] %(name)s - %(message)s
datefmt=
class=messageSub.lib.loghandler.ContextFormatter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment