Log ERROR to stderr and rest to stdout. (#576)

This commit is contained in:
Darko Poljak 2017-09-13 13:06:06 +02:00 committed by GitHub
parent f2614469c5
commit 08bc38dd0f
3 changed files with 45 additions and 8 deletions

View file

@ -148,9 +148,7 @@ class Config(object):
if args.parallel or args.jobs: if args.parallel or args.jobs:
# If parallel execution then also log process id # If parallel execution then also log process id
del logging.getLogger().handlers[:] cdist.log.setupParallelLogging()
log_format = '%(levelname)s: [%(process)d]: %(message)s'
logging.basicConfig(format=log_format)
log = logging.getLogger("cdist") log = logging.getLogger("cdist")
if args.parallel: if args.parallel:

View file

@ -21,6 +21,7 @@
# #
import logging import logging
import sys
# Define additional cdist logging levels. # Define additional cdist logging levels.
@ -48,14 +49,38 @@ def _trace(msg, *args, **kwargs):
logging.trace = _trace 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): def __init__(self, name):
self.name = name
super().__init__(name) super().__init__(name)
formatter = logging.Formatter(self.FORMAT)
self.addFilter(self) 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): def filter(self, record):
"""Prefix messages with logger name""" """Prefix messages with logger name"""
@ -70,5 +95,18 @@ class Log(logging.Logger):
self.log(logging.TRACE, msg, *args, **kwargs) self.log(logging.TRACE, msg, *args, **kwargs)
logging.setLoggerClass(Log) class ParallelLog(DefaultLog):
logging.basicConfig(format='%(levelname)s: %(message)s') 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()

View file

@ -17,6 +17,7 @@ next:
* Core: Add -l/--log-level option (Darko Poljak) * Core: Add -l/--log-level option (Darko Poljak)
* Type __install_stage: Fix __debug -> __cdist_log_level (Darko Poljak) * Type __install_stage: Fix __debug -> __cdist_log_level (Darko Poljak)
* Documentation: Document __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 4.6.1: 2017-08-30
* Type __user: Explore with /etc files (passwd, group, shadow) (Philippe Gregoire) * Type __user: Explore with /etc files (passwd, group, shadow) (Philippe Gregoire)