comparison src/core/log_config.py @ 1129:08f50fdac21b

core (logging): new backend_data parameter can be used to transmit specific data to a backend + Twisted backend use this option to know if we are in debug or nodaemon mode
author Goffi <goffi@goffi.org>
date Mon, 25 Aug 2014 17:21:03 +0200
parents 64ff259d3cbb
children 069ad98b360d
comparison
equal deleted inserted replaced
1128:c7fe4fe66fbc 1129:08f50fdac21b
186 self.manageOutputs(output) 186 self.manageOutputs(output)
187 addObserver = self.twisted_log.addObserver 187 addObserver = self.twisted_log.addObserver
188 188
189 if C.LOG_OPT_OUTPUT_DEFAULT in log.handlers: 189 if C.LOG_OPT_OUTPUT_DEFAULT in log.handlers:
190 # default output is already managed, we just add output to stdout if we are in debug or nodaemon mode 190 # default output is already managed, we just add output to stdout if we are in debug or nodaemon mode
191 191 if self.backend_data is None:
192 def postOptions(options): # called after options are parsed 192 raise ValueError("You must pass options as backend_data with Twisted backend")
193 if options.get('nodaemon', False) or options.get('debug', False): 193 options = self.backend_data
194 addObserver(self.twisted_log.FileLogObserver(sys.stdout).emit) 194 if options.get('nodaemon', False) or options.get('debug', False):
195 195 addObserver(self.twisted_log.FileLogObserver(sys.stdout).emit)
196 # XXX: this hack is to know if we are in debug or nodaemon mode
197 # any better method welcomed
198 from twisted.scripts import twistd
199 # we monkey patch ServerOptions to be sure options.get is done after options are fully parsed
200 # XXX: only works with twistd plugins, not with .tac files !
201 twistd.ServerOptions.postOptions = postOptions
202 else: 196 else:
203 # \\default is not in the output, so we remove current observers 197 # \\default is not in the output, so we remove current observers
204 self.cleared_observers = self.log_publisher.observers 198 self.cleared_observers = self.log_publisher.observers
205 self.observers.clear() 199 self.observers.clear()
206 del self.log_publisher.observers[:] 200 del self.log_publisher.observers[:]
239 self.log_publisher._originalAddObserver(twistedObserver) 233 self.log_publisher._originalAddObserver(twistedObserver)
240 234
241 235
242 class ConfigureStandard(ConfigureBasic): 236 class ConfigureStandard(ConfigureBasic):
243 237
244 def __init__(self, level=None, fmt=None, output=None, logger=None, colors=False, force_colors=False): 238 def __init__(self, level=None, fmt=None, output=None, logger=None, colors=False, force_colors=False, backend_data=None):
245 if fmt is None: 239 if fmt is None:
246 fmt = C.LOG_OPT_FORMAT[1] 240 fmt = C.LOG_OPT_FORMAT[1]
247 if output is None: 241 if output is None:
248 output = C.LOG_OPT_OUTPUT[1] 242 output = C.LOG_OPT_OUTPUT[1]
249 super(ConfigureStandard, self).__init__(level, fmt, output, logger, colors, force_colors) 243 super(ConfigureStandard, self).__init__(level, fmt, output, logger, colors, force_colors, backend_data)
250 244
251 def preTreatment(self): 245 def preTreatment(self):
252 """We use logging methods directly, instead of using Logger""" 246 """We use logging methods directly, instead of using Logger"""
253 import logging 247 import logging
254 log.getLogger = logging.getLogger 248 log.getLogger = logging.getLogger
378 level = options[LEVEL].upper() 372 level = options[LEVEL].upper()
379 if level not in C.LOG_LEVELS: 373 if level not in C.LOG_LEVELS:
380 level = C.LOG_LVL_INFO 374 level = C.LOG_LVL_INFO
381 options[LEVEL] = level 375 options[LEVEL] = level
382 376
383 def satConfigure(backend=C.LOG_BACKEND_STANDARD, const=None): 377 def satConfigure(backend=C.LOG_BACKEND_STANDARD, const=None, backend_data=None):
384 """Configure logging system for SàT, can be used by frontends 378 """Configure logging system for SàT, can be used by frontends
385 379
386 logs conf is read in SàT conf, then in environment variables. It must be done before Memory init 380 logs conf is read in SàT conf, then in environment variables. It must be done before Memory init
387 @param backend: backend to use, it can be: 381 @param backend: backend to use, it can be:
388 - C.LOG_BACKEND_BASIC: print based backend 382 - C.LOG_BACKEND_BASIC: print based backend
407 log_conf[opt_name] = config.get(C.LOG_OPT_SECTION, C.LOG_OPT_PREFIX + opt_name) 401 log_conf[opt_name] = config.get(C.LOG_OPT_SECTION, C.LOG_OPT_PREFIX + opt_name)
408 except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): 402 except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
409 log_conf[opt_name] = opt_default 403 log_conf[opt_name] = opt_default
410 404
411 _parseOptions(log_conf) 405 _parseOptions(log_conf)
412 configure(backend, **log_conf) 406 configure(backend, backend_data=backend_data, **log_conf)