Browse Source

Fix emulator colored logging

6.6
Darko Poljak 2 years ago
parent
commit
840e417eb7
  1. 2
      cdist/argparse.py
  2. 3
      cdist/core/manifest.py
  3. 2
      cdist/emulator.py
  4. 1
      cdist/flock.py
  5. 16
      cdist/log.py

2
cdist/argparse.py vendored

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

3
cdist/core/manifest.py vendored

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

2
cdist/emulator.py vendored

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

1
cdist/flock.py vendored

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

16
cdist/log.py vendored

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

Loading…
Cancel
Save