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): def handle_log_colors(args):
if cdist.configuration.ColoredOutputOption.translate(args.colored_output): 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): 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': util.log_level_env_var_val(self.log),
'__cdist_log_level_name': util.log_level_name_env_var_val( '__cdist_log_level_name': util.log_level_name_env_var_val(
self.log), self.log),
'__cdist_colored_log': str(self.log.USE_COLORS).lower(), '__cdist_colored_log': str(
cdist.log.CdistFormatter.USE_COLORS).lower(),
} }
if dry_run: if dry_run:

View file

@ -130,7 +130,7 @@ class Emulator(object):
logging.root.setLevel(logging.WARNING) logging.root.setLevel(logging.WARNING)
colored_log = self.env.get('__cdist_colored_log', 'false') 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]) self.log = logging.getLogger(self.target_host[0])

View file

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

View file

@ -50,7 +50,8 @@ def _trace(msg, *args, **kwargs):
logging.trace = _trace logging.trace = _trace
class ColorFormatter(logging.Formatter): class CdistFormatter(logging.Formatter):
USE_COLORS = False
RESET = '\033[0m' RESET = '\033[0m'
COLOR_MAP = { COLOR_MAP = {
'ERROR': '\033[0;31m', 'ERROR': '\033[0;31m',
@ -66,6 +67,7 @@ class ColorFormatter(logging.Formatter):
def format(self, record): def format(self, record):
msg = super().format(record) msg = super().format(record)
if self.USE_COLORS:
color = self.COLOR_MAP.get(record.levelname) color = self.COLOR_MAP.get(record.levelname)
if color: if color:
msg = color + msg + self.RESET msg = color + msg + self.RESET
@ -73,7 +75,6 @@ class ColorFormatter(logging.Formatter):
class DefaultLog(logging.Logger): class DefaultLog(logging.Logger):
USE_COLORS = False
FORMAT = '%(levelname)s: %(message)s' FORMAT = '%(levelname)s: %(message)s'
class StdoutFilter(logging.Filter): class StdoutFilter(logging.Filter):
@ -88,10 +89,7 @@ class DefaultLog(logging.Logger):
super().__init__(name) super().__init__(name)
self.propagate = False self.propagate = False
if self.USE_COLORS: formatter = CdistFormatter(self.FORMAT)
formatter = ColorFormatter(self.FORMAT)
else:
formatter = logging.Formatter(self.FORMAT)
self.addFilter(self) self.addFilter(self)