Browse Source

celery.utils.compat.LoggerAdapter: Compat version of logging.LoggerAdapter introduced in Python 2.6.

Ask Solem 15 years ago
parent
commit
0bbe5595ea
2 changed files with 53 additions and 6 deletions
  1. 2 1
      celery/log.py
  2. 51 5
      celery/utils/compat.py

+ 2 - 1
celery/log.py

@@ -8,6 +8,7 @@ import traceback
 from celery import conf
 from celery.utils import noop
 from celery.utils.patch import ensure_process_aware_logger
+from celery.utils.compat import LoggerAdapter
 
 _hijacked = False
 _monkeypatched = False
@@ -89,7 +90,7 @@ def setup_task_logger(loglevel=conf.CELERYD_LOG_LEVEL, logfile=None,
     task_kwargs.setdefault("task_name", "-?-")
     logger = _setup_logger(get_task_logger(loglevel),
                            logfile, format, **kwargs)
-    return logging.LoggerAdapter(logger, task_kwargs)
+    return LoggerAdapter(logger, task_kwargs)
 
 
 def _setup_logger(logger, logfile, format,

+ 51 - 5
celery/utils/compat.py

@@ -1,11 +1,13 @@
 from __future__ import generators
-############## parse_qsl ####################################################
+
+############## urlparse.parse_qsl ###########################################
+
 try:
     from urlparse import parse_qsl
 except ImportError:
     from cgi import parse_qsl
 
-############## all ##########################################################
+############## __builtin__.all ##############################################
 
 try:
     all([True])
@@ -17,7 +19,7 @@ except NameError:
                 return False
         return True
 
-############## any ##########################################################
+############## __builtin__.any ##############################################
 
 try:
     any([True])
@@ -29,7 +31,7 @@ except NameError:
                 return True
         return False
 
-############## OrderedDict ##################################################
+############## collections.OrderedDict ######################################
 
 import weakref
 try:
@@ -240,7 +242,7 @@ class OrderedDict(dict, MutableMapping):
     def __ne__(self, other):
         return not (self == other)
 
-############## defaultdict ##################################################
+############## collections.defaultdict ######################################
 
 try:
     from collections import defaultdict
@@ -286,3 +288,47 @@ except ImportError:
                                             dict.__repr__(self))
     import collections
     collections.defaultdict = defaultdict # Pickle needs this.
+
+############## logging.LoggerAdapter ########################################
+
+try:
+    from logging import LoggerAdapter
+except ImportError:
+    class LoggerAdapter(object):
+
+        def __init__(self, logger, extra):
+            self.logger = logger
+            self.extra = extra
+
+        def process(self, msg, kwargs):
+            kwargs["extra"] = self.extra
+            return msg, kwargs
+
+        def debug(self, msg, *args, **kwargs):
+            msg, kwargs = self.process(msg, kwargs)
+            self.logger.debug(msg, *args, **kwargs)
+
+        def info(self, msg, *args, **kwargs):
+            msg, kwargs = self.process(msg, kwargs)
+            self.logger.info(msg, *args, **kwargs)
+
+        def warning(self, msg, *args, **kwargs):
+            msg, kwargs = self.process(msg, kwargs)
+            self.logger.warning(msg, *args, **kwargs)
+
+        def error(self, msg, *args, **kwargs):
+            msg, kwargs = self.process(msg, kwargs)
+            self.logger.error(msg, *args, **kwargs)
+
+        def exception(self, msg, *args, **kwargs):
+            msg, kwargs = self.process(msg, kwargs)
+            kwargs["exc_info"] = 1
+            self.logger.error(msg, *args, **kwargs)
+
+        def critical(self, msg, *args, **kwargs):
+            msg, kwargs = self.process(msg, kwargs)
+            self.logger.critical(msg, *args, **kwargs)
+
+        def log(self, level, msg, *args, **kwargs):
+            msg, kwargs = self.process(msg, kwargs)
+            self.logger.log(level, msg, *args, **kwargs)