Skip to content

Instantly share code, notes, and snippets.

@ttback
Last active December 23, 2015 11:18
Show Gist options
  • Save ttback/6626999 to your computer and use it in GitHub Desktop.
Save ttback/6626999 to your computer and use it in GitHub Desktop.
Simple Custom Log Handlers in python
import custom_logger
import logging
import logging.handlers
class Runner ():
def __init__(self):
pass
def run(self):
#logging.basicConfig(filename="Task.log",filemode='w', fmt = '%(asctime)s - %(levelname)s - %(module)s - %(message)s',datefmt = '%m/%d/%Y %I:%M:%S %p', level=logging.DEBUG)
logger = logging.getLogger(__name__)
ch=custom_logger.CustomLogger()
fmt= logging.Formatter(fmt = '%(asctime)s - %(levelname)s - %(module)s - %(message)s',datefmt = '%m/%d/%Y %I:%M:%S %p')
ch.setFormatter(fmt)
ch.setLevel(logging.DEBUG)
logger.setLevel(logging.DEBUG)
logger.addHandler(ch)
logger.debug('Test 1')
logger.info('Test 2')
logger.warning('Test 3')
logger.error('Test 4')
logger.critical('Test 5')
ch.complete()
runner = Runner()
runner.run()
import logging
class CustomLogger (logging.Handler):
def __init__(self):
logging.Handler.__init__(self)
self.records = []
def emit (self, record):
self.records.append(self.format(record))
#print self.format(record)
def complete(self):
print '\n'.join(self.records)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment