primitivus: use of new logging system:
- default output is \\memory
- logs can be seen with :messages command
- now useless writeLog method has been removed
This commit is contained in:
parent
468206e529
commit
39989cc985
3 changed files with 80 additions and 43 deletions
|
|
@ -79,6 +79,7 @@ class Const(object):
|
|||
LOG_BACKEND_BASIC = 'basic'
|
||||
LOG_BASE_LOGGER = 'root'
|
||||
LOG_TWISTED_LOGGER = 'twisted'
|
||||
LOG_OPT_SECTION = 'DEFAULT' # section of sat.conf where log options should be
|
||||
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
|
||||
|
|
@ -91,7 +92,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_OPTIONS = (LOG_OPT_COLORS, LOG_OPT_LEVEL, LOG_OPT_FORMAT, LOG_OPT_LOGGER, LOG_OPT_OUTPUT)
|
||||
LOG_LVL_DEBUG = 'DEBUG'
|
||||
LOG_LVL_INFO = 'INFO'
|
||||
LOG_LVL_WARNING = 'WARNING'
|
||||
|
|
@ -115,3 +115,10 @@ class Const(object):
|
|||
ANSI_BOLD = '\033[1m'
|
||||
ANSI_BLINK = '\033[5m'
|
||||
ANSI_BLINK_OFF = '\033[25m'
|
||||
|
||||
@classmethod
|
||||
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)
|
||||
|
||||
|
|
|
|||
|
|
@ -169,6 +169,18 @@ class FilterName(object):
|
|||
log_record.name = dict_record['name']
|
||||
return self.filter(log_record) == 1
|
||||
|
||||
def memoryGet(size=None):
|
||||
"""Return buffered logs
|
||||
|
||||
@param size: number of logs to return
|
||||
"""
|
||||
if not C.LOG_OPT_OUTPUT_MEMORY in _handlers:
|
||||
raise ValueError('memory output is not used')
|
||||
if _backend == C.LOG_BACKEND_STANDARD:
|
||||
mem_handler = _handlers[C.LOG_OPT_OUTPUT_MEMORY]
|
||||
return (log_msg for log_msg in mem_handler.buffer[size if size is None else -size:])
|
||||
else:
|
||||
raise NotImplementedError
|
||||
|
||||
def _getProfile():
|
||||
"""Try to find profile value using introspection"""
|
||||
|
|
@ -509,7 +521,11 @@ class ConfigureTwisted(ConfigureBasic):
|
|||
|
||||
class ConfigureStandard(Configure):
|
||||
|
||||
def __init__(self, level=None, fmt=C.LOG_OPT_FORMAT[1], output=C.LOG_OPT_OUTPUT[1], logger=None, colors=False, force_colors=False):
|
||||
def __init__(self, level=None, fmt=None, output=None, logger=None, colors=False, force_colors=False):
|
||||
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)
|
||||
|
||||
def preTreatment(self):
|
||||
|
|
@ -559,7 +575,7 @@ class ConfigureStandard(Configure):
|
|||
|
||||
def configureColors(self, colors, force_colors):
|
||||
import sys
|
||||
self.with_color = colors & (sys.stdout.isatty() or force_colors)
|
||||
self.formatter.with_color = colors & (sys.stdout.isatty() or force_colors)
|
||||
if not colors and force_colors:
|
||||
raise ValueError("force_colors can't be used if colors is False")
|
||||
|
||||
|
|
@ -579,8 +595,11 @@ class ConfigureStandard(Configure):
|
|||
hdlr = logging.StreamHandler()
|
||||
self._addHandler(root_logger, hdlr)
|
||||
elif handler == C.LOG_OPT_OUTPUT_MEMORY:
|
||||
import logging.handlers
|
||||
hdlr = logging.handlers.BufferingHandler(options)
|
||||
from logging.handlers import BufferingHandler
|
||||
class SatMemoryHandler(BufferingHandler):
|
||||
def emit(self, record):
|
||||
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)
|
||||
elif handler == C.LOG_OPT_OUTPUT_FILE:
|
||||
|
|
@ -640,23 +659,31 @@ def _parseOptions(options):
|
|||
level = C.LOG_LVL_INFO
|
||||
options[LEVEL] = level
|
||||
|
||||
def satConfigure(backend=C.LOG_BACKEND_TWISTED):
|
||||
def satConfigure(backend=C.LOG_BACKEND_STANDARD, const=None):
|
||||
"""Configure logging system for SàT, can be used by frontends
|
||||
|
||||
logs conf is read in SàT conf, then in environment variables. It must be done before Memory init
|
||||
@param backend: backend to use, it can be:
|
||||
- C.LOG_BACKEND_BASIC: print based backend
|
||||
- C.LOG_BACKEND_TWISTED: Twisted logging backend
|
||||
- C.LOG_BACKEND_STANDARD: standard logging backend
|
||||
@param const: Const class to use instead of sat.core.constants.Const (mainly used to change default values)
|
||||
"""
|
||||
if const is not None:
|
||||
global C
|
||||
C = const
|
||||
import ConfigParser
|
||||
import os
|
||||
log_conf = {}
|
||||
config = ConfigParser.SafeConfigParser()
|
||||
config.read(C.CONFIG_FILES)
|
||||
for opt_name, opt_default in C.LOG_OPTIONS:
|
||||
for opt_name, opt_default in C.LOG_OPTIONS():
|
||||
try:
|
||||
log_conf[opt_name] = os.environ[''.join((C.ENV_PREFIX, C.LOG_OPT_PREFIX.upper(), opt_name.upper()))]
|
||||
except KeyError:
|
||||
try:
|
||||
log_conf[opt_name] = config.get('DEFAULT', C.LOG_OPT_PREFIX + opt_name)
|
||||
except ConfigParser.NoOptionError:
|
||||
log_conf[opt_name] = config.get(C.LOG_OPT_SECTION, C.LOG_OPT_PREFIX + opt_name)
|
||||
except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
|
||||
log_conf[opt_name] = opt_default
|
||||
|
||||
_parseOptions(log_conf)
|
||||
|
|
|
|||
|
|
@ -22,8 +22,9 @@ from twisted.internet import glib2reactor
|
|||
glib2reactor.install()
|
||||
|
||||
# XXX: We need to configure logs before any log method is used, so here is the best place.
|
||||
from sat.core.constants import Const as C
|
||||
from sat.core import log
|
||||
log.satConfigure()
|
||||
log.satConfigure(C.LOG_BACKEND_TWISTED)
|
||||
|
||||
# XXX: SAT must be imported after log configuration, because it write stuff to logs
|
||||
from sat.core.sat_main import SAT
|
||||
|
|
|
|||
Loading…
Reference in a new issue