Skip to content

Instantly share code, notes, and snippets.

@UBarney
Created July 8, 2017 12:52
Show Gist options
  • Save UBarney/d1e82ea878e79959e612bcdc21ae44cd to your computer and use it in GitHub Desktop.
Save UBarney/d1e82ea878e79959e612bcdc21ae44cd to your computer and use it in GitHub Desktop.
set_log_filter
import logging, sys
class SingleLevelFilter(logging.Filter):
def __init__(self, passlevel, reject):
self.passlevel = passlevel
self.reject = reject
def filter(self, record):
if self.reject:
return (record.levelno != self.passlevel)
else:
return (record.levelno == self.passlevel)
h1 = logging.StreamHandler(sys.stdout)
f1 = SingleLevelFilter(logging.INFO, False)
h1.addFilter(f1)
rootLogger = logging.getLogger()
rootLogger.addHandler(h1)
h2 = logging.StreamHandler(sys.stderr)
f2 = SingleLevelFilter(logging.INFO, True)
h2.addFilter(f2)
rootLogger.addHandler(h2)
logger = logging.getLogger("my.logger")
logger.setLevel(logging.DEBUG)
logger.debug("A DEBUG message")
logger.info("An INFO message")
logger.warning("A WARNING message")
logger.error("An ERROR message")
logger.critical("A CRITICAL message")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment