From 08bc38dd0fc85e88b65d3068827346686a1e20f0 Mon Sep 17 00:00:00 2001 From: Darko Poljak Date: Wed, 13 Sep 2017 13:06:06 +0200 Subject: [PATCH] Log ERROR to stderr and rest to stdout. (#576) --- cdist/config.py | 4 +--- cdist/log.py | 48 +++++++++++++++++++++++++++++++++++++++++++----- docs/changelog | 1 + 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/cdist/config.py b/cdist/config.py index ed69e3e4..06db807a 100644 --- a/cdist/config.py +++ b/cdist/config.py @@ -148,9 +148,7 @@ class Config(object): if args.parallel or args.jobs: # If parallel execution then also log process id - del logging.getLogger().handlers[:] - log_format = '%(levelname)s: [%(process)d]: %(message)s' - logging.basicConfig(format=log_format) + cdist.log.setupParallelLogging() log = logging.getLogger("cdist") if args.parallel: diff --git a/cdist/log.py b/cdist/log.py index ce0addcc..dba1ad2f 100644 --- a/cdist/log.py +++ b/cdist/log.py @@ -21,6 +21,7 @@ # import logging +import sys # Define additional cdist logging levels. @@ -48,14 +49,38 @@ def _trace(msg, *args, **kwargs): logging.trace = _trace -class Log(logging.Logger): +class DefaultLog(logging.Logger): + + FORMAT = '%(levelname)s: %(message)s' + + class StdoutFilter(logging.Filter): + def filter(self, rec): + return rec.levelno != logging.ERROR + + class StderrFilter(logging.Filter): + def filter(self, rec): + return rec.levelno == logging.ERROR def __init__(self, name): - - self.name = name super().__init__(name) + + formatter = logging.Formatter(self.FORMAT) + self.addFilter(self) + stdout_handler = logging.StreamHandler(sys.stdout) + stdout_handler.addFilter(self.StdoutFilter()) + stdout_handler.setLevel(logging.TRACE) + stdout_handler.setFormatter(formatter) + + stderr_handler = logging.StreamHandler(sys.stderr) + stderr_handler.addFilter(self.StderrFilter()) + stderr_handler.setLevel(logging.ERROR) + stderr_handler.setFormatter(formatter) + + self.addHandler(stdout_handler) + self.addHandler(stderr_handler) + def filter(self, record): """Prefix messages with logger name""" @@ -70,5 +95,18 @@ class Log(logging.Logger): self.log(logging.TRACE, msg, *args, **kwargs) -logging.setLoggerClass(Log) -logging.basicConfig(format='%(levelname)s: %(message)s') +class ParallelLog(DefaultLog): + FORMAT = '%(levelname)s: [%(process)d]: %(message)s' + + +def setupDefaultLogging(): + del logging.getLogger().handlers[:] + logging.setLoggerClass(DefaultLog) + + +def setupParallelLogging(): + del logging.getLogger().handlers[:] + logging.setLoggerClass(ParallelLog) + + +setupDefaultLogging() diff --git a/docs/changelog b/docs/changelog index ddf4a685..1df6af21 100644 --- a/docs/changelog +++ b/docs/changelog @@ -17,6 +17,7 @@ next: * Core: Add -l/--log-level option (Darko Poljak) * Type __install_stage: Fix __debug -> __cdist_log_level (Darko Poljak) * Documentation: Document __cdist_log_level (Darko Poljak) + * Core: Log ERROR to stderr and rest to stdout (Darko Poljak, Steven Armstrong) 4.6.1: 2017-08-30 * Type __user: Explore with /etc files (passwd, group, shadow) (Philippe Gregoire)