Fix emulator colored logging

This commit is contained in:
Darko Poljak 2020-06-11 14:16:37 +02:00
parent 74e5d7182a
commit 840e417eb7
5 changed files with 12 additions and 12 deletions

View file

@ -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):

View file

@ -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:

View file

@ -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])

View file

@ -22,6 +22,7 @@
import fcntl
import logging
import os
import cdist.log
log = logging.getLogger('cdist-flock')

View file

@ -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,6 +67,7 @@ class ColorFormatter(logging.Formatter):
def format(self, record):
msg = super().format(record)
if self.USE_COLORS:
color = self.COLOR_MAP.get(record.levelname)
if color:
msg = color + msg + self.RESET
@ -73,7 +75,6 @@ class ColorFormatter(logging.Formatter):
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)