Przeglądaj źródła

Log colors: Use safe_str before adding colors

Ask Solem 14 lat temu
rodzic
commit
c76d7318ee
3 zmienionych plików z 21 dodań i 17 usunięć
  1. 1 1
      celery/log.py
  2. 19 0
      celery/utils/encoding.py
  3. 1 16
      celery/worker/job.py

+ 1 - 1
celery/log.py

@@ -39,7 +39,7 @@ class ColorFormatter(logging.Formatter):
         color = self.colors.get(levelname)
 
         if self.use_color and color:
-            record.msg = unicode(color(record.msg))
+            record.msg = unicode(color(safe_str(record.msg)))
 
         # Very ugly, but have to make sure processName is supported
         # by foreign logger instances.

+ 19 - 0
celery/utils/encoding.py

@@ -0,0 +1,19 @@
+import sys
+
+
+def default_encoding():
+    if sys.platform.startswith("java"):
+        return "utf-8"
+    return sys.getfilesystemencoding()
+
+
+def safe_str(s, errors="replace"):
+    encoding = default_encoding()
+    try:
+        if isinstance(s, unicode):
+            return s.encode(encoding, errors)
+        return unicode(s, encoding, errors)
+    except Exception:
+        return "<Unrepresentable %r>" % (type(s), )
+
+

+ 1 - 16
celery/worker/job.py

@@ -17,6 +17,7 @@ from celery.registry import tasks
 from celery.utils import noop, kwdict, fun_takes_kwargs
 from celery.utils import truncate_text
 from celery.utils.compat import log_with_extra
+from celery.utils.encoding import safe_str
 from celery.utils.timeutils import maybe_iso8601
 from celery.worker import state
 
@@ -55,22 +56,6 @@ class AlreadyExecutedError(Exception):
     world-wide state."""
 
 
-def default_encoding():
-    if sys.platform.startswith("java"):
-        return "utf-8"
-    return sys.getfilesystemencoding()
-
-
-def safe_str(s, errors="replace"):
-    encoding = default_encoding()
-    try:
-        if isinstance(s, unicode):
-            return s.encode(encoding, errors)
-        return unicode(s, encoding, errors)
-    except Exception:
-        return "<Unrepresentable %r>" % (type(s), )
-
-
 class WorkerTaskTrace(TaskTrace):
     """Wraps the task in a jail, catches all exceptions, and
     saves the status and result of the task execution to the task