diff src/core/log.py @ 1017:0ea97f483464

core (log): added "custom" backend
author Goffi <goffi@goffi.org>
date Thu, 08 May 2014 19:11:20 +0200
parents 0c361fdc76af
children a836b6da2c5c
line wrap: on
line diff
--- a/src/core/log.py	Thu May 08 17:16:15 2014 +0200
+++ b/src/core/log.py	Thu May 08 19:11:20 2014 +0200
@@ -242,7 +242,7 @@
 
 
 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 @@
         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 @@
         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 @@
     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 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))