Skip to content

Instantly share code, notes, and snippets.

@krinkere krinkere/logging.py
Last active Mar 9, 2017

Embed
What would you like to do?
Example of adding a filter to a Logger's Handler. Enables messages at one specifie level only
"""
Example of adding a filter to a Logger's Handler.
"""
from logging import getLogger, FileHandler, INFO, ERROR
class LogOneLevel:
"""Enables messages at one specifie level only"""
def __init__(self, level):
self.level = level
def filter(self, log_record):
return log_record.levelno == self.level
logger = getLogger('logger_demo')
info_handler = FileHandler('info.log')
info_handler.addFilter(LogOneLevel(INFO)) # add filter to handler
logger.addHandler(info_handler)
error_handler = FileHandler('error.log')
error_handler.addFilter(LogOneLevel(ERROR)) # add filter to handler
logger.addHandler(error_handler)
logger.info("info msg") # written to info.log only
logger.error("error msg") # written to error.log only
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.