Created
July 8, 2017 12:52
-
-
Save UBarney/d1e82ea878e79959e612bcdc21ae44cd to your computer and use it in GitHub Desktop.
set_log_filter
This file contains 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 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