core (log): configuration and environment variables are now checked for log level and colors:

- variable change logs behaviour, so far only level and colors are implemented
    - configuration use log_[name], for example you can put log_level=debug in sat.conf (section [DEFAULT]) to see all levels
    - environment variables use SAT_LOG_[NAME]: e.g. SAT_LOG_LEVEL=debug
    - colors can be true, false or force to force colors even if stdout is not a tty
This commit is contained in:
Goffi 2014-04-19 20:11:23 +02:00
commit 317342b616

View file

@ -106,7 +106,7 @@ def _configureStdLogging(logging, level=None, colors=False, force_colors=False):
else:
root_logger.warning(u"Handler already set on root logger")
def configure(backend=C.LOG_BACKEND_STANDARD):
def configure(backend=C.LOG_BACKEND_STANDARD, **options):
"""Configure logging bejaviour
@param backend: can be:
C.LOG_BACKEND_STANDARD: use standard logging module
@ -130,7 +130,7 @@ def configure(backend=C.LOG_BACKEND_STANDARD):
global error
global critical
import logging
_configureStdLogging(logging, colors=True)
_configureStdLogging(logging, **options)
getLogger = logging.getLogger
debug = logging.debug
info = logging.info
@ -144,6 +144,47 @@ def configure(backend=C.LOG_BACKEND_STANDARD):
else:
raise ValueError("unknown backend")
def _parseOptions(options):
"""Parse string options as given in conf or environment variable, and return expected python value
@param options (dict): options with (key: name, value: string value)
"""
if 'colors' in options:
if options['colors'].lower() in ('1', 'true'):
options['colors'] = True
elif options['colors'] == 'force':
options['colors'] = True
options['force_colors'] = True
else:
options['colors'] = False
if 'level' in options:
level = options['level'].upper()
if level not in ('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'):
level = 'INFO'
options['level'] = level
def satConfigure(backend=C.LOG_BACKEND_TWISTED):
"""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
"""
import ConfigParser
import os
to_get = (C.LOG_OPT_COLORS, C.LOG_OPT_LEVEL)
log_conf = {}
config = ConfigParser.SafeConfigParser()
config.read(C.CONFIG_FILES)
for opt_name, opt_default in to_get:
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] = opt_default
_parseOptions(log_conf)
configure(backend, **log_conf)
def getLogger(name=C.LOG_BASE_LOGGER):
return _loggers.setdefault(name, Logger(name))