core (log): added "custom" backend
This commit is contained in:
parent
2aeada884e
commit
9905e14d0c
2 changed files with 18 additions and 3 deletions
|
|
@ -77,6 +77,7 @@ class Const(object):
|
|||
LOG_BACKEND_STANDARD = 'standard'
|
||||
LOG_BACKEND_TWISTED = 'twisted'
|
||||
LOG_BACKEND_BASIC = 'basic'
|
||||
LOG_BACKEND_CUSTOM = 'custom'
|
||||
LOG_BASE_LOGGER = 'root'
|
||||
LOG_TWISTED_LOGGER = 'twisted'
|
||||
LOG_OPT_SECTION = 'DEFAULT' # section of sat.conf where log options should be
|
||||
|
|
|
|||
|
|
@ -242,7 +242,7 @@ def _ansiColors(level, message):
|
|||
|
||||
|
||||
class Configure(object):
|
||||
LOGGER_CLASS = None
|
||||
LOGGER_CLASS = Logger
|
||||
|
||||
def __init__(self, level=None, fmt=None, output=None, logger=None, colors=False, force_colors=False):
|
||||
"""Configure backend
|
||||
|
|
@ -524,6 +524,13 @@ class ConfigureTwisted(ConfigureBasic):
|
|||
self.log_publisher._originalAddObserver(twistedObserver)
|
||||
|
||||
|
||||
class ConfigureCustom(ConfigureBasic):
|
||||
LOGGER_CLASS = None
|
||||
|
||||
def __init__(self, logger_class, *args, **kwargs):
|
||||
ConfigureCustom.LOGGER_CLASS = logger_class
|
||||
|
||||
|
||||
class ConfigureStandard(Configure):
|
||||
|
||||
def __init__(self, level=None, fmt=None, output=None, logger=None, colors=False, force_colors=False):
|
||||
|
|
@ -632,6 +639,7 @@ def configure(backend=C.LOG_BACKEND_STANDARD, **options):
|
|||
C.LOG_BACKEND_STANDARD: use standard logging module
|
||||
C.LOG_BACKEND_TWISTED: use twisted logging module (with standard logging observer)
|
||||
C.LOG_BACKEND_BASIC: use a basic print based logging
|
||||
C.LOG_BACKEND_CUSTOM: use a given Logger subclass
|
||||
"""
|
||||
global _backend
|
||||
if _backend is not None:
|
||||
|
|
@ -647,6 +655,10 @@ def configure(backend=C.LOG_BACKEND_STANDARD, **options):
|
|||
elif backend == C.LOG_BACKEND_STANDARD:
|
||||
ConfigureStandard(**options)
|
||||
|
||||
elif backend == C.LOG_BACKEND_CUSTOM:
|
||||
logger_class = options.pop('logger_class')
|
||||
ConfigureCustom(logger_class, **options)
|
||||
|
||||
else:
|
||||
raise ValueError("unknown backend")
|
||||
|
||||
|
|
@ -704,9 +716,11 @@ def satConfigure(backend=C.LOG_BACKEND_STANDARD, const=None):
|
|||
|
||||
def getLogger(name=C.LOG_BASE_LOGGER):
|
||||
if _backend in (None, C.LOG_BACKEND_BASIC):
|
||||
logger_class = Logger
|
||||
logger_class = ConfigureBasic.LOGGER_CLASS
|
||||
elif _backend == C.LOG_BACKEND_TWISTED:
|
||||
logger_class = TwistedLogger
|
||||
logger_class = ConfigureTwisted.LOGGER_CLASS
|
||||
elif _backend == C.LOG_BACKEND_CUSTOM:
|
||||
logger_class = ConfigureCustom.LOGGER_CLASS
|
||||
else:
|
||||
raise ValueError("This method should not be called with backend [%s]" % _backend)
|
||||
return _loggers.setdefault(name, logger_class(name))
|
||||
|
|
|
|||
Loading…
Reference in a new issue