core (logs): taints:
log taints can be specified using log_levels_taints_dict in sat.conf, where key is a level name, and value is a list of color or code names. For instance, the following conf would taint INFO level in bold/green and DEBUG in default color:
log_levels_taints_dict = {"debug": [], "info": ["green", "bold"]}
Unknown names are copied as raw strings.
This commit is contained in:
parent
864520086a
commit
1e05785e83
3 changed files with 56 additions and 37 deletions
|
|
@ -194,6 +194,12 @@ class Const(object):
|
|||
|
||||
|
||||
## Logging ##
|
||||
LOG_LVL_DEBUG = 'DEBUG'
|
||||
LOG_LVL_INFO = 'INFO'
|
||||
LOG_LVL_WARNING = 'WARNING'
|
||||
LOG_LVL_ERROR = 'ERROR'
|
||||
LOG_LVL_CRITICAL = 'CRITICAL'
|
||||
LOG_LEVELS = (LOG_LVL_DEBUG, LOG_LVL_INFO, LOG_LVL_WARNING, LOG_LVL_ERROR, LOG_LVL_CRITICAL)
|
||||
LOG_BACKEND_STANDARD = 'standard'
|
||||
LOG_BACKEND_TWISTED = 'twisted'
|
||||
LOG_BACKEND_BASIC = 'basic'
|
||||
|
|
@ -204,6 +210,13 @@ class Const(object):
|
|||
LOG_OPT_PREFIX = 'log_'
|
||||
# (option_name, default_value) tuples
|
||||
LOG_OPT_COLORS = ('colors', 'true') # true for auto colors, force to have colors even if stdout is not a tty, false for no color
|
||||
LOG_OPT_TAINTS_DICT = ('levels_taints_dict', {
|
||||
LOG_LVL_DEBUG: ('cyan',),
|
||||
LOG_LVL_INFO: (),
|
||||
LOG_LVL_WARNING: ('yellow',),
|
||||
LOG_LVL_ERROR: ('red', 'blink', r'/!\ ', 'blink_off'),
|
||||
LOG_LVL_CRITICAL: ('bold', 'red', 'Guru Meditation ', 'normal_weight')
|
||||
})
|
||||
LOG_OPT_LEVEL = ('level', 'info')
|
||||
LOG_OPT_FORMAT = ('fmt', '%(message)s') # similar to logging format.
|
||||
LOG_OPT_LOGGER = ('logger', '') # regex to filter logger name
|
||||
|
|
@ -213,12 +226,6 @@ class Const(object):
|
|||
LOG_OPT_OUTPUT_MEMORY_LIMIT = 50
|
||||
LOG_OPT_OUTPUT_FILE = 'file' # file is implicit if only output
|
||||
LOG_OPT_OUTPUT = ('output', LOG_OPT_OUTPUT_SEP + LOG_OPT_OUTPUT_DEFAULT) # //default = normal output (stderr or a file with twistd), path/to/file for a file (must be the first if used), //memory for memory (options can be put in parenthesis, e.g.: //memory(500) for a 500 lines memory)
|
||||
LOG_LVL_DEBUG = 'DEBUG'
|
||||
LOG_LVL_INFO = 'INFO'
|
||||
LOG_LVL_WARNING = 'WARNING'
|
||||
LOG_LVL_ERROR = 'ERROR'
|
||||
LOG_LVL_CRITICAL = 'CRITICAL'
|
||||
LOG_LEVELS = (LOG_LVL_DEBUG, LOG_LVL_INFO, LOG_LVL_WARNING, LOG_LVL_ERROR, LOG_LVL_CRITICAL)
|
||||
|
||||
|
||||
## action constants ##
|
||||
|
|
@ -263,7 +270,7 @@ class Const(object):
|
|||
def LOG_OPTIONS(cls):
|
||||
"""Return options checked for logs"""
|
||||
# XXX: we use a classmethod so we can use Const inheritance to change default options
|
||||
return(cls.LOG_OPT_COLORS, cls.LOG_OPT_LEVEL, cls.LOG_OPT_FORMAT, cls.LOG_OPT_LOGGER, cls.LOG_OPT_OUTPUT)
|
||||
return(cls.LOG_OPT_COLORS, cls.LOG_OPT_TAINTS_DICT, cls.LOG_OPT_LEVEL, cls.LOG_OPT_FORMAT, cls.LOG_OPT_LOGGER, cls.LOG_OPT_OUTPUT)
|
||||
|
||||
@classmethod
|
||||
def bool(cls, value):
|
||||
|
|
|
|||
|
|
@ -159,7 +159,7 @@ class ConfigureBase(object):
|
|||
LOGGER_CLASS = Logger
|
||||
_color_location = False # True if color location is specified in fmt (with COLOR_START)
|
||||
|
||||
def __init__(self, level=None, fmt=None, output=None, logger=None, colors=False, force_colors=False, backend_data=None):
|
||||
def __init__(self, level=None, fmt=None, output=None, logger=None, colors=False, levels_taints_dict=None, force_colors=False, backend_data=None):
|
||||
"""Configure a backend
|
||||
|
||||
@param level: one of C.LOG_LEVELS
|
||||
|
|
@ -178,7 +178,7 @@ class ConfigureBase(object):
|
|||
self.configureFormat(fmt)
|
||||
self.configureOutput(output)
|
||||
self.configureLogger(logger)
|
||||
self.configureColors(colors, force_colors)
|
||||
self.configureColors(colors, force_colors, levels_taints_dict)
|
||||
self.postTreatment()
|
||||
self.updateCurrentLogger()
|
||||
|
||||
|
|
@ -221,8 +221,29 @@ class ConfigureBase(object):
|
|||
if logger:
|
||||
Logger.filter_name = FilterName(logger)
|
||||
|
||||
def configureColors(self, colors, force_colors):
|
||||
pass
|
||||
def configureColors(self, colors, force_colors, levels_taints_dict):
|
||||
if colors:
|
||||
# if color are used, we need to handle levels_taints_dict
|
||||
for level in levels_taints_dict.keys():
|
||||
# we wants levels in uppercase to correspond to contstants
|
||||
levels_taints_dict[level.upper()] = levels_taints_dict[level]
|
||||
taints = self.__class__.taints = {}
|
||||
for level in C.LOG_LEVELS:
|
||||
# we want use values and use constant value as default
|
||||
taint_list = levels_taints_dict.get(level, C.LOG_OPT_TAINTS_DICT[1][level])
|
||||
ansi_list = []
|
||||
for elt in taint_list:
|
||||
elt = elt.upper()
|
||||
try:
|
||||
ansi = getattr(C, 'ANSI_FG_{}'.format(elt))
|
||||
except AttributeError:
|
||||
try:
|
||||
ansi = getattr(C, 'ANSI_{}'.format(elt))
|
||||
except AttributeError:
|
||||
# we use raw string if element is unknown
|
||||
ansi = elt
|
||||
ansi_list.append(ansi)
|
||||
taints[level] = ''.join(ansi_list)
|
||||
|
||||
def postTreatment(self):
|
||||
pass
|
||||
|
|
@ -289,29 +310,17 @@ class ConfigureBase(object):
|
|||
@param message: formatted message to log
|
||||
@return: message with ANSI escape codes for coloration
|
||||
"""
|
||||
if level == C.LOG_LVL_DEBUG:
|
||||
start = (C.ANSI_FG_CYAN,)
|
||||
elif level == C.LOG_LVL_INFO:
|
||||
start = ()
|
||||
elif level == C.LOG_LVL_WARNING:
|
||||
start = (C.ANSI_FG_YELLOW,)
|
||||
elif level == C.LOG_LVL_ERROR:
|
||||
start = (C.ANSI_FG_RED,
|
||||
C.ANSI_BLINK,
|
||||
r'/!\ ',
|
||||
C.ANSI_BLINK_OFF)
|
||||
elif level == C.LOG_LVL_CRITICAL:
|
||||
start = (C.ANSI_BOLD,
|
||||
C.ANSI_FG_RED,
|
||||
'Guru Meditation ',
|
||||
C.ANSI_NORMAL_WEIGHT)
|
||||
else:
|
||||
start = ()
|
||||
|
||||
try:
|
||||
start = cls.taints[level]
|
||||
except KeyError:
|
||||
start = ''
|
||||
|
||||
if cls._color_location:
|
||||
return message % {'color_start': ''.join(start),
|
||||
return message % {'color_start': start,
|
||||
'color_end': C.ANSI_RESET}
|
||||
else:
|
||||
return '%s%s%s' % (''.join(start), message, C.ANSI_RESET)
|
||||
return '%s%s%s' % (start, message, C.ANSI_RESET)
|
||||
|
||||
@staticmethod
|
||||
def getProfile():
|
||||
|
|
|
|||
|
|
@ -43,12 +43,13 @@ class TwistedLogger(log.Logger):
|
|||
|
||||
class ConfigureBasic(log.ConfigureBase):
|
||||
|
||||
def configureColors(self, colors, force_colors):
|
||||
def configureColors(self, colors, force_colors, levels_taints_dict):
|
||||
super(ConfigureBasic, self).configureColors(colors, force_colors, levels_taints_dict)
|
||||
if colors:
|
||||
import sys
|
||||
if force_colors or sys.stdout.isatty(): # FIXME: isatty should be tested on each handler, not globaly
|
||||
# we need colors
|
||||
log.Logger.post_treat = lambda self, level, message: self.ansiColors(level, message)
|
||||
log.Logger.post_treat = lambda logger, level, message: self.ansiColors(level, message)
|
||||
elif force_colors:
|
||||
raise ValueError("force_colors can't be used if colors is False")
|
||||
|
||||
|
|
@ -210,7 +211,8 @@ class ConfigureTwisted(ConfigureBasic):
|
|||
if C.LOG_OPT_OUTPUT_MEMORY in log.handlers:
|
||||
raise NotImplementedError("Memory observer is not implemented in Twisted backend")
|
||||
|
||||
def configureColors(self, colors, force_colors):
|
||||
def configureColors(self, colors, force_colors, levels_taints_dict):
|
||||
super(ConfigureTwisted, self).configureColors(colors, force_colors, levels_taints_dict)
|
||||
self.LOGGER_CLASS.colors = colors
|
||||
self.LOGGER_CLASS.force_colors = force_colors
|
||||
if force_colors and not colors:
|
||||
|
|
@ -235,12 +237,12 @@ class ConfigureTwisted(ConfigureBasic):
|
|||
|
||||
class ConfigureStandard(ConfigureBasic):
|
||||
|
||||
def __init__(self, level=None, fmt=None, output=None, logger=None, colors=False, force_colors=False, backend_data=None):
|
||||
def __init__(self, level=None, fmt=None, output=None, logger=None, colors=False, levels_taints_dict=None, force_colors=False, backend_data=None):
|
||||
if fmt is None:
|
||||
fmt = C.LOG_OPT_FORMAT[1]
|
||||
if output is None:
|
||||
output = C.LOG_OPT_OUTPUT[1]
|
||||
super(ConfigureStandard, self).__init__(level, fmt, output, logger, colors, force_colors, backend_data)
|
||||
super(ConfigureStandard, self).__init__(level, fmt, output, logger, colors, levels_taints_dict, force_colors, backend_data)
|
||||
|
||||
def preTreatment(self):
|
||||
"""We use logging methods directly, instead of using Logger"""
|
||||
|
|
@ -295,7 +297,8 @@ class ConfigureStandard(ConfigureBasic):
|
|||
def configureLogger(self, logger):
|
||||
self.name_filter = log.FilterName(logger) if logger else None
|
||||
|
||||
def configureColors(self, colors, force_colors):
|
||||
def configureColors(self, colors, force_colors, levels_taints_dict):
|
||||
super(ConfigureStandard, self).configureColors(colors, force_colors, levels_taints_dict)
|
||||
self.formatterClass.with_colors = colors
|
||||
self.formatterClass.force_colors = force_colors
|
||||
if not colors and force_colors:
|
||||
|
|
|
|||
Loading…
Reference in a new issue