core(log): standard backend: colors are now checked on a per handler basis
This commit is contained in:
parent
2fddb02872
commit
ad9c41cbc2
1 changed files with 24 additions and 19 deletions
|
|
@ -373,17 +373,17 @@ class ConfigureBasic(Configure):
|
|||
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 @@ class ConfigureTwisted(ConfigureBasic):
|
|||
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 @@ class ConfigureTwisted(ConfigureBasic):
|
|||
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 @@ class ConfigureStandard(Configure):
|
|||
|
||||
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 @@ class ConfigureStandard(Configure):
|
|||
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 @@ class ConfigureStandard(Configure):
|
|||
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 @@ class ConfigureStandard(Configure):
|
|||
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:
|
||||
|
|
|
|||
Loading…
Reference in a new issue