# HG changeset patch # User Goffi # Date 1399313768 -7200 # Node ID c8771279497edb962e40c5ea7a24a1bf35d8726d # Parent 5a6354ff468c382596c91007ea3b903fd06e73df core(log): standard backend: colors are now checked on a per handler basis diff -r 5a6354ff468c -r c8771279497e src/core/log.py --- a/src/core/log.py Mon May 05 20:12:21 2014 +0200 +++ b/src/core/log.py Mon May 05 20:16:08 2014 +0200 @@ -373,17 +373,17 @@ class ConfigureTwisted(ConfigureBasic): LOGGER_CLASS = TwistedLogger - def changeObserver(self, observer, can_color=False): + def changeObserver(self, observer, can_colors=False): """Install a hook on observer to manage SàT specificities @param observer: original observer to hook - @param can_color: True if observer can display ansi colors + @param can_colors: True if observer can display ansi colors """ def observer_hook(event): """redirect non SàT log to twisted_logger, and add colors when possible""" if 'sat_logged' in event: # we only want our own logs, other are managed by twistedObserver # we add colors if possible - if (can_color and self.LOGGER_CLASS.colors) or self.LOGGER_CLASS.force_colors: + if (can_colors and self.LOGGER_CLASS.colors) or self.LOGGER_CLASS.force_colors: message = event.get('message', tuple()) level = event.get('level', C.LOG_LVL_INFO) if message: @@ -401,10 +401,10 @@ log_obs = observer.__self__ log_file = log_obs.write.__self__ try: - can_color = log_file.isatty() + can_colors = log_file.isatty() except AttributeError: - can_color = False - return self.changeObserver(observer, can_color=can_color) + can_colors = False + return self.changeObserver(observer, can_colors=can_colors) def installObserverHook(self, observer): """Check observer type and install SàT hook when possible @@ -417,7 +417,7 @@ if isinstance(observer.__self__, self.log.FileLogObserver): observer = self.changeFileLogObserver(observer) elif isinstance(observer.__self__, self.log.DefaultObserver): - observer = self.changeObserver(observer, can_color=True) + observer = self.changeObserver(observer, can_colors=True) else: # we use print because log system is not fully initialized print("Unmanaged observer [%s]" % observer) @@ -551,21 +551,22 @@ def configureFormat(self, fmt): import logging - format_ = fmt class SatFormatter(logging.Formatter): u"""Formatter which manage SàT specificities""" + _format = fmt - def __init__(self, fmt=None, datefmt=None): - super(SatFormatter, self).__init__(fmt, datefmt) + def __init__(self, can_colors=False): + super(SatFormatter, self).__init__(self._format) + self.can_colors = can_colors def format(self, record): s = super(SatFormatter, self).format(record) - if self.with_color: + if self.with_colors and (self.can_colors or self.force_colors): s = _ansiColors(record.levelname, s) return s - self.formatter = SatFormatter(format_) + self.formatterClass = SatFormatter def configureOutput(self, output): self.manageOutputs(output) @@ -574,13 +575,13 @@ self.name_filter = FilterName(logger) if logger else None def configureColors(self, colors, force_colors): - import sys - self.formatter.with_color = colors & (sys.stdout.isatty() or force_colors) + self.formatterClass.with_colors = colors + self.formatterClass.force_colors = force_colors if not colors and force_colors: raise ValueError("force_colors can't be used if colors is False") - def _addHandler(self, root_logger, hdlr): - hdlr.setFormatter(self.formatter) + def _addHandler(self, root_logger, hdlr, can_colors=False): + hdlr.setFormatter(self.formatterClass(can_colors)) root_logger.addHandler(hdlr) root_logger.setLevel(self.level) if self.name_filter is not None: @@ -593,7 +594,11 @@ for handler, options in _handlers.items(): if handler == C.LOG_OPT_OUTPUT_DEFAULT: hdlr = logging.StreamHandler() - self._addHandler(root_logger, hdlr) + try: + can_colors = hdlr.stream.isatty() + except AttributeError: + can_colors = False + self._addHandler(root_logger, hdlr, can_colors=can_colors) elif handler == C.LOG_OPT_OUTPUT_MEMORY: from logging.handlers import BufferingHandler class SatMemoryHandler(BufferingHandler): @@ -601,12 +606,12 @@ super(SatMemoryHandler, self).emit(self.format(record)) hdlr = SatMemoryHandler(options) _handlers[handler] = hdlr # we keep a reference to the handler to read the buffer later - self._addHandler(root_logger, hdlr) + self._addHandler(root_logger, hdlr, can_colors=False) elif handler == C.LOG_OPT_OUTPUT_FILE: import os.path for path in options: hdlr = logging.FileHandler(os.path.expanduser(path)) - self._addHandler(root_logger, hdlr) + self._addHandler(root_logger, hdlr, can_colors=False) else: raise ValueError("Unknown handler type") else: