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
3 changed files with 60 additions and 5 deletions

View file

@ -34,6 +34,7 @@ class Const(object):
APP_VERSION = u'0.4.1D' # Please add 'D' at the end for dev versions
APP_URL = 'http://salut-a-toi.org'
## Parameters ##
NO_SECURITY_LIMIT = -1
INDIVIDUAL = "individual"
@ -47,6 +48,8 @@ class Const(object):
ENTITY_LAST_RESOURCE = 'LAST_RESOURCE'
ENTITY_CAP_HASH = 'CAP_HASH'
## Configuration ##
if BaseDirectory: # skipped when imported from Libervia browser_side
## Configuration ##
@ -66,21 +69,30 @@ class Const(object):
CONFIG_FILES = [(os.path.expanduser(path) + 'sat.conf') for path in \
['/etc/', '~/', '~/.', '', '.'] + \
['%s/' % path for path in list(BaseDirectory.load_config_paths('sat'))]
]
]
## Plugins ##
# names of widely used plugins
TEXT_CMDS = 'TEXT-COMMANDS'
## Logging ##
LOG_BACKEND_STANDARD = 'standard'
LOG_BACKEND_TWISTED = 'twisted'
LOG_BACKEND_BASIC = 'basic'
LOG_BASE_LOGGER = 'root'
LOG_OPT_PREFIX = 'log_'
# (option_name, default_value) tuples
LOG_OPT_COLORS = ('colors', 'true')
LOG_OPT_LEVEL = ('level', 'info')
## Misc ##
SAVEFILE_DATABASE = "sat.db"
IQ_SET = '/iq[@type="set"]'
ENV_PREFIX = 'SAT_' # Prefix used for environment variables
## ANSI escape sequences ##
# XXX: used for logging

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))

View file

@ -21,12 +21,14 @@ from twisted.application import service
from twisted.internet import glib2reactor
glib2reactor.install()
from sat.core.constants import Const as C
# XXX: We need to configure logs before any log method is used, so here is the best place.
from sat.core import log
log.configure(C.LOG_BACKEND_TWISTED)
log.satConfigure()
# XXX: SAT must be imported after log configuration, because it write stuff to logs
from sat.core.sat_main import SAT
application = service.Application('SàT')
service = SAT()
service.setServiceParent(application)