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

This commit is contained in:
Goffi 2014-08-25 17:21:03 +02:00
commit d549533f00
2 changed files with 11 additions and 16 deletions

View file

@ -155,7 +155,7 @@ class FilterName(object):
class ConfigureBase(object):
LOGGER_CLASS = Logger
def __init__(self, level=None, fmt=None, output=None, logger=None, colors=False, force_colors=False):
def __init__(self, level=None, fmt=None, output=None, logger=None, colors=False, force_colors=False, backend_data=None):
"""Configure a backend
@param level: one of C.LOG_LEVELS
@ -168,6 +168,7 @@ class ConfigureBase(object):
@param colors: if True use ANSI colors to show log levels
@param force_colors: if True ANSI colors are used even if stdout is not a tty
"""
self.backend_data = backend_data
self.preTreatment()
self.configureLevel(level)
self.configureFormat(fmt)

View file

@ -188,17 +188,11 @@ class ConfigureTwisted(ConfigureBasic):
if C.LOG_OPT_OUTPUT_DEFAULT in log.handlers:
# default output is already managed, we just add output to stdout if we are in debug or nodaemon mode
def postOptions(options): # called after options are parsed
if options.get('nodaemon', False) or options.get('debug', False):
addObserver(self.twisted_log.FileLogObserver(sys.stdout).emit)
# XXX: this hack is to know if we are in debug or nodaemon mode
# any better method welcomed
from twisted.scripts import twistd
# we monkey patch ServerOptions to be sure options.get is done after options are fully parsed
# XXX: only works with twistd plugins, not with .tac files !
twistd.ServerOptions.postOptions = postOptions
if self.backend_data is None:
raise ValueError("You must pass options as backend_data with Twisted backend")
options = self.backend_data
if options.get('nodaemon', False) or options.get('debug', False):
addObserver(self.twisted_log.FileLogObserver(sys.stdout).emit)
else:
# \\default is not in the output, so we remove current observers
self.cleared_observers = self.log_publisher.observers
@ -241,12 +235,12 @@ class ConfigureTwisted(ConfigureBasic):
class ConfigureStandard(ConfigureBasic):
def __init__(self, level=None, fmt=None, output=None, logger=None, colors=False, force_colors=False):
def __init__(self, level=None, fmt=None, output=None, logger=None, colors=False, force_colors=False, backend_data=None):
if fmt is None:
fmt = C.LOG_OPT_FORMAT[1]
if output is None:
output = C.LOG_OPT_OUTPUT[1]
super(ConfigureStandard, self).__init__(level, fmt, output, logger, colors, force_colors)
super(ConfigureStandard, self).__init__(level, fmt, output, logger, colors, force_colors, backend_data)
def preTreatment(self):
"""We use logging methods directly, instead of using Logger"""
@ -380,7 +374,7 @@ def _parseOptions(options):
level = C.LOG_LVL_INFO
options[LEVEL] = level
def satConfigure(backend=C.LOG_BACKEND_STANDARD, const=None):
def satConfigure(backend=C.LOG_BACKEND_STANDARD, const=None, backend_data=None):
"""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
@ -409,4 +403,4 @@ def satConfigure(backend=C.LOG_BACKEND_STANDARD, const=None):
log_conf[opt_name] = opt_default
_parseOptions(log_conf)
configure(backend, **log_conf)
configure(backend, backend_data=backend_data, **log_conf)