comparison src/core/log.py @ 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 f51a1895275c
children b4af31a8a4f2
comparison
equal deleted inserted replaced
993:301b342c697a 994:652c01ca69b1
104 root_logger.addHandler(hdlr) 104 root_logger.addHandler(hdlr)
105 root_logger.setLevel(level) 105 root_logger.setLevel(level)
106 else: 106 else:
107 root_logger.warning(u"Handler already set on root logger") 107 root_logger.warning(u"Handler already set on root logger")
108 108
109 def configure(backend=C.LOG_BACKEND_STANDARD): 109 def configure(backend=C.LOG_BACKEND_STANDARD, **options):
110 """Configure logging bejaviour 110 """Configure logging bejaviour
111 @param backend: can be: 111 @param backend: can be:
112 C.LOG_BACKEND_STANDARD: use standard logging module 112 C.LOG_BACKEND_STANDARD: use standard logging module
113 C.LOG_BACKEND_TWISTED: use twisted logging module (with standard logging observer) 113 C.LOG_BACKEND_TWISTED: use twisted logging module (with standard logging observer)
114 C.LOG_BACKEND_BASIC: use a basic print based logging 114 C.LOG_BACKEND_BASIC: use a basic print based logging
128 global info 128 global info
129 global warning 129 global warning
130 global error 130 global error
131 global critical 131 global critical
132 import logging 132 import logging
133 _configureStdLogging(logging, colors=True) 133 _configureStdLogging(logging, **options)
134 getLogger = logging.getLogger 134 getLogger = logging.getLogger
135 debug = logging.debug 135 debug = logging.debug
136 info = logging.info 136 info = logging.info
137 warning = logging.warning 137 warning = logging.warning
138 error = logging.error 138 error = logging.error
142 pass 142 pass
143 143
144 else: 144 else:
145 raise ValueError("unknown backend") 145 raise ValueError("unknown backend")
146 146
147 def _parseOptions(options):
148 """Parse string options as given in conf or environment variable, and return expected python value
149
150 @param options (dict): options with (key: name, value: string value)
151 """
152 if 'colors' in options:
153 if options['colors'].lower() in ('1', 'true'):
154 options['colors'] = True
155 elif options['colors'] == 'force':
156 options['colors'] = True
157 options['force_colors'] = True
158 else:
159 options['colors'] = False
160 if 'level' in options:
161 level = options['level'].upper()
162 if level not in ('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'):
163 level = 'INFO'
164 options['level'] = level
165
166 def satConfigure(backend=C.LOG_BACKEND_TWISTED):
167 """Configure logging system for SàT, can be used by frontends
168
169 logs conf is read in SàT conf, then in environment variables. It must be done before Memory init
170 """
171 import ConfigParser
172 import os
173 to_get = (C.LOG_OPT_COLORS, C.LOG_OPT_LEVEL)
174 log_conf = {}
175 config = ConfigParser.SafeConfigParser()
176 config.read(C.CONFIG_FILES)
177 for opt_name, opt_default in to_get:
178 try:
179 log_conf[opt_name] = os.environ[''.join((C.ENV_PREFIX, C.LOG_OPT_PREFIX.upper(), opt_name.upper()))]
180 except KeyError:
181 try:
182 log_conf[opt_name] = config.get('DEFAULT', C.LOG_OPT_PREFIX + opt_name)
183 except ConfigParser.NoOptionError:
184 log_conf[opt_name] = opt_default
185
186 _parseOptions(log_conf)
187 configure(backend, **log_conf)
147 188
148 def getLogger(name=C.LOG_BASE_LOGGER): 189 def getLogger(name=C.LOG_BASE_LOGGER):
149 return _loggers.setdefault(name, Logger(name)) 190 return _loggers.setdefault(name, Logger(name))
150 191
151 _root_logger = getLogger() 192 _root_logger = getLogger()