From 840e417eb79c7135762d3353f34a3f8330cb0e84 Mon Sep 17 00:00:00 2001 From: Darko Poljak Date: Thu, 11 Jun 2020 14:16:37 +0200 Subject: [PATCH] Fix emulator colored logging --- cdist/argparse.py | 2 +- cdist/core/manifest.py | 3 ++- cdist/emulator.py | 2 +- cdist/flock.py | 1 + cdist/log.py | 16 +++++++--------- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/cdist/argparse.py b/cdist/argparse.py index 85dba246..1c83237c 100644 --- a/cdist/argparse.py +++ b/cdist/argparse.py @@ -498,7 +498,7 @@ def handle_loglevel(args): def handle_log_colors(args): if cdist.configuration.ColoredOutputOption.translate(args.colored_output): - cdist.log.DefaultLog.USE_COLORS = True + cdist.log.CdistFormatter.USE_COLORS = True def parse_and_configure(argv, singleton=True): diff --git a/cdist/core/manifest.py b/cdist/core/manifest.py index 8b833ff2..2e7b873e 100644 --- a/cdist/core/manifest.py +++ b/cdist/core/manifest.py @@ -119,7 +119,8 @@ class Manifest(object): '__cdist_log_level': util.log_level_env_var_val(self.log), '__cdist_log_level_name': util.log_level_name_env_var_val( self.log), - '__cdist_colored_log': str(self.log.USE_COLORS).lower(), + '__cdist_colored_log': str( + cdist.log.CdistFormatter.USE_COLORS).lower(), } if dry_run: diff --git a/cdist/emulator.py b/cdist/emulator.py index 4eaf2c93..08e15a78 100644 --- a/cdist/emulator.py +++ b/cdist/emulator.py @@ -130,7 +130,7 @@ class Emulator(object): logging.root.setLevel(logging.WARNING) colored_log = self.env.get('__cdist_colored_log', 'false') - cdist.log.ColorFormatter.USE_COLORS = colored_log == 'true' + cdist.log.CdistFormatter.USE_COLORS = colored_log == 'true' self.log = logging.getLogger(self.target_host[0]) diff --git a/cdist/flock.py b/cdist/flock.py index d8bac916..3afacfd9 100644 --- a/cdist/flock.py +++ b/cdist/flock.py @@ -22,6 +22,7 @@ import fcntl import logging import os +import cdist.log log = logging.getLogger('cdist-flock') diff --git a/cdist/log.py b/cdist/log.py index 19efebdb..efae2cd3 100644 --- a/cdist/log.py +++ b/cdist/log.py @@ -50,7 +50,8 @@ def _trace(msg, *args, **kwargs): logging.trace = _trace -class ColorFormatter(logging.Formatter): +class CdistFormatter(logging.Formatter): + USE_COLORS = False RESET = '\033[0m' COLOR_MAP = { 'ERROR': '\033[0;31m', @@ -66,14 +67,14 @@ class ColorFormatter(logging.Formatter): def format(self, record): msg = super().format(record) - color = self.COLOR_MAP.get(record.levelname) - if color: - msg = color + msg + self.RESET + if self.USE_COLORS: + color = self.COLOR_MAP.get(record.levelname) + if color: + msg = color + msg + self.RESET return msg class DefaultLog(logging.Logger): - USE_COLORS = False FORMAT = '%(levelname)s: %(message)s' class StdoutFilter(logging.Filter): @@ -88,10 +89,7 @@ class DefaultLog(logging.Logger): super().__init__(name) self.propagate = False - if self.USE_COLORS: - formatter = ColorFormatter(self.FORMAT) - else: - formatter = logging.Formatter(self.FORMAT) + formatter = CdistFormatter(self.FORMAT) self.addFilter(self)