Mercurial > libervia-backend
changeset 994:652c01ca69b1
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
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 19 Apr 2014 20:11:23 +0200 |
parents | 301b342c697a |
children | 4dbe8e57ff51 |
files | src/core/constants.py src/core/log.py src/sat.tac |
diffstat | 3 files changed, 60 insertions(+), 5 deletions(-) [+] |
line wrap: on
line diff
--- a/src/core/constants.py Sat Apr 19 19:19:19 2014 +0200 +++ b/src/core/constants.py Sat Apr 19 20:11:23 2014 +0200 @@ -34,6 +34,7 @@ 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 @@ 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 @@ 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
--- a/src/core/log.py Sat Apr 19 19:19:19 2014 +0200 +++ b/src/core/log.py Sat Apr 19 20:11:23 2014 +0200 @@ -106,7 +106,7 @@ 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 @@ 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 @@ 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))
--- a/src/sat.tac Sat Apr 19 19:19:19 2014 +0200 +++ b/src/sat.tac Sat Apr 19 20:11:23 2014 +0200 @@ -21,12 +21,14 @@ 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)