Переглянути джерело

Logging should now handle utf8 correctly

David Cramer 14 роки тому
батько
коміт
00a30f462c
2 змінених файлів з 13 додано та 2 видалено
  1. 12 2
      celery/log.py
  2. 1 0
      celery/worker/job.py

+ 12 - 2
celery/log.py

@@ -3,6 +3,7 @@ import logging
 import threading
 import threading
 import sys
 import sys
 import traceback
 import traceback
+import types
 
 
 from multiprocessing import current_process
 from multiprocessing import current_process
 from multiprocessing import util as mputil
 from multiprocessing import util as mputil
@@ -31,18 +32,27 @@ class ColorFormatter(logging.Formatter):
         logging.Formatter.__init__(self, msg)
         logging.Formatter.__init__(self, msg)
         self.use_color = use_color
         self.use_color = use_color
 
 
+    def formatException(self, ei):
+        r = logging.Formatter.formatException(self, ei)
+        if type(r) in [types.StringType]:
+            r = r.decode('utf-8', 'replace') # Convert to unicode
+        return r
+
     def format(self, record):
     def format(self, record):
         levelname = record.levelname
         levelname = record.levelname
 
 
         if self.use_color and levelname in COLORS:
         if self.use_color and levelname in COLORS:
-            record.msg = str(colored().names[COLORS[levelname]](record.msg))
+            record.msg = unicode(colored().names[COLORS[levelname]](record.msg))
 
 
         # Very ugly, but have to make sure processName is supported
         # Very ugly, but have to make sure processName is supported
         # by foreign logger instances.
         # by foreign logger instances.
         # (processName is always supported by Python 2.7)
         # (processName is always supported by Python 2.7)
         if "processName" not in record.__dict__:
         if "processName" not in record.__dict__:
             record.__dict__["processName"] = current_process()._name
             record.__dict__["processName"] = current_process()._name
-        return logging.Formatter.format(self, record)
+        t = logging.Formatter.format(self, record)
+        if type(t) in [types.UnicodeType]:
+            t = t.encode('utf-8', 'replace')
+        return t
 
 
 
 
 class Logging(object):
 class Logging(object):

+ 1 - 0
celery/worker/job.py

@@ -474,6 +474,7 @@ class TaskRequest(object):
                    "kwargs": self.kwargs}
                    "kwargs": self.kwargs}
 
 
         self.logger.error(self.error_msg.strip() % context,
         self.logger.error(self.error_msg.strip() % context,
+                          exc_info=exc_info,
                           extra={
                           extra={
                               "data": {
                               "data": {
                                   "hostname": self.hostname,
                                   "hostname": self.hostname,