Print logging output to correct stream
Info, and debug messages are going to stdout, warning, errors
are going to stderr.
Fixes
@@ -16,6 +16,19 @@ from dogen.plugin import Plugin
import colorlog
+# Source: http://stackoverflow.com/questions/1383254/logging-streamhandler-and-standard-streams
+# Adjusted
+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)
+
class MyParser(argparse.ArgumentParser):
def error(self, message):
@@ -26,13 +39,21 @@ class MyParser(argparse.ArgumentParser):
class CLI(object):
def __init__(self):
+ handler_out = logging.StreamHandler(sys.stdout)
+ handler_err = logging.StreamHandler(sys.stderr)
+
+ handler_out.addFilter(SingleLevelFilter(logging.INFO, False))
+ handler_err.addFilter(SingleLevelFilter(logging.INFO, True))
+
formatter = colorlog.ColoredFormatter(
'%(log_color)s%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
- handler = logging.StreamHandler()
- handler.setFormatter(formatter)
+
+ handler_out.setFormatter(formatter)
+ handler_err.setFormatter(formatter)
self.log = logging.getLogger("dogen")
- self.log.addHandler(handler)
+ self.log.addHandler(handler_out)
+ self.log.addHandler(handler_err)
for package in ["requests.packages.urllib3", "pykwalify.rule"]:
log = logging.getLogger(package)
