[Zope3-checkins] SVN: Zope3/trunk/src/zope/exceptions/ Added a subclass of logging.Formatter, so that the logging framework can use

Shane Hathaway shane at zope.com
Wed Jan 18 23:37:01 EST 2006


Log message for revision 41359:
  Added a subclass of logging.Formatter, so that the logging framework can use
  the special traceback formatter.  Also tidied the exception formatter by
  removing the getRevision stuff that probably no one knew was there. :-)
  

Changed:
  U   Zope3/trunk/src/zope/exceptions/exceptionformatter.py
  A   Zope3/trunk/src/zope/exceptions/log.py

-=-
Modified: Zope3/trunk/src/zope/exceptions/exceptionformatter.py
===================================================================
--- Zope3/trunk/src/zope/exceptions/exceptionformatter.py	2006-01-19 01:43:24 UTC (rev 41358)
+++ Zope3/trunk/src/zope/exceptions/exceptionformatter.py	2006-01-19 04:36:59 UTC (rev 41359)
@@ -19,6 +19,7 @@
 import sys
 import cgi
 import linecache
+import traceback
 
 DEBUG_EXCEPTION_FORMATTER = 1
 
@@ -27,37 +28,22 @@
     line_sep = '\n'
     show_revisions = 0
 
-    def __init__(self, limit=None):
+    def __init__(self, limit=None, with_filenames=False):
         self.limit = limit
+        self.with_filenames = with_filenames
 
     def escape(self, s):
         return s
 
     def getPrefix(self):
-        return 'Traceback (innermost last):'
+        return 'Traceback (most recent call last):'
 
     def getLimit(self):
         limit = self.limit
         if limit is None:
-            limit = getattr(sys, 'tracebacklimit', None)
+            limit = getattr(sys, 'tracebacklimit', 200)
         return limit
 
-    def getRevision(self, globals):
-        if not self.show_revisions:
-            return None
-        revision = globals.get('__revision__', None)
-        if revision is None:
-            # Incorrect but commonly used spelling
-            revision = globals.get('__version__', None)
-
-        if revision is not None:
-            try:
-                revision = str(revision).strip()
-            except (ValueError, AttributeError):
-                # Just in case revisions cannot be converted to strings.
-                revision = '???'
-        return revision
-
     def formatSupplementLine(self, line):
         return '   - %s' % line
 
@@ -122,14 +108,13 @@
         name = co.co_name
         locals = f.f_locals
         globals = f.f_globals
-        modname = globals.get('__name__', filename)
 
-        s = '  Module %s, line %d' % (modname, lineno)
+        if self.with_filenames:
+            s = '  File "%s", line %d' % (filename, lineno)
+        else:
+            modname = globals.get('__name__', filename)
+            s = '  Module %s, line %d' % (modname, lineno)
 
-        revision = self.getRevision(globals)
-        if revision:
-            s = s + ', rev. %s' % revision
-
         s = s + ', in %s' % name
 
         result = []
@@ -158,7 +143,6 @@
                 result.extend(self.formatSupplement(supp, tb))
             except:
                 if DEBUG_EXCEPTION_FORMATTER:
-                    import traceback
                     traceback.print_exc()
                 # else just swallow the exception.
 
@@ -168,14 +152,12 @@
                 result.append(self.formatTracebackInfo(tbi))
         except:
             if DEBUG_EXCEPTION_FORMATTER:
-                import traceback
                 traceback.print_exc()
             # else just swallow the exception.
 
         return self.line_sep.join(result)
 
     def formatExceptionOnly(self, etype, value):
-        import traceback
         return self.line_sep.join(
             traceback.format_exception_only(etype, value))
 
@@ -210,7 +192,7 @@
         return cgi.escape(s)
 
     def getPrefix(self):
-        return '<p>Traceback (innermost last):\r\n<ul>'
+        return '<p>Traceback (most recent call last):\r\n<ul>'
 
     def formatSupplementLine(self, line):
         return '<b>%s</b>' % self.escape(str(line))
@@ -228,18 +210,31 @@
         return '</ul>%s</p>' % self.escape(exc_line)
 
 
-limit = 200
+def format_exception(t, v, tb, limit=None, as_html=False,
+                     with_filenames=False):
+    """Format a stack trace and the exception information.
 
-if hasattr(sys, 'tracebacklimit'):
-    limit = min(limit, sys.tracebacklimit)
-
-text_formatter = TextExceptionFormatter(limit)
-html_formatter = HTMLExceptionFormatter(limit)
-
-
-def format_exception(t, v, tb, limit=None, as_html=0):
+    Similar to 'traceback.format_exception', but adds supplemental
+    information to the traceback and accepts two options, 'as_html'
+    and 'with_filenames'.
+    """
     if as_html:
-        fmt = html_formatter
+        fmt = HTMLExceptionFormatter(limit, with_filenames)
     else:
-        fmt = text_formatter
+        fmt = TextExceptionFormatter(limit, with_filenames)
     return fmt.formatException(t, v, tb)
+
+
+def print_exception(t, v, tb, limit=None, file=None, as_html=False,
+                    with_filenames=True):
+    """Print exception up to 'limit' stack trace entries from 'tb' to 'file'.
+
+    Similar to 'traceback.print_exception', but adds supplemental
+    information to the traceback and accepts two options, 'as_html'
+    and 'with_filenames'.
+    """
+    if file is None:
+        file = sys.stderr
+    lines = format_exception(t, v, tb, limit, as_html, with_filenames)
+    for line in lines:
+        file.write(line)

Added: Zope3/trunk/src/zope/exceptions/log.py
===================================================================
--- Zope3/trunk/src/zope/exceptions/log.py	2006-01-19 01:43:24 UTC (rev 41358)
+++ Zope3/trunk/src/zope/exceptions/log.py	2006-01-19 04:36:59 UTC (rev 41359)
@@ -0,0 +1,35 @@
+##############################################################################
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Log formatter that enhances tracebacks with extra information.
+
+$Id: $
+"""
+
+import logging
+import cStringIO
+from zope.exceptions.exceptionformatter import print_exception
+
+class Formatter(logging.Formatter):
+
+    def formatException(self, ei):
+        """Format and return the specified exception information as a string.
+
+        Uses zope.exceptions.exceptionformatter to generate the traceback.
+        """
+        sio = cStringIO.StringIO()
+        print_exception(ei[0], ei[1], ei[2], file=sio, with_filenames=True)
+        s = sio.getvalue()
+        if s.endswith("\n"):
+            s = s[:-1]
+        return s



More information about the Zope3-Checkins mailing list