[Zope-Checkins] CVS: Zope/lib/python/Products/SiteErrorLog - SiteErrorLog.py:1.1.2.2

Shane Hathaway shane@cvs.zope.org
Wed, 3 Apr 2002 14:45:04 -0500


Update of /cvs-repository/Zope/lib/python/Products/SiteErrorLog
In directory cvs.zope.org:/tmp/cvs-serv7194

Modified Files:
      Tag: shane-better-tracebacks-branch
	SiteErrorLog.py 
Log Message:
The site error log product now shows the log and the tracebacks through the
web.


=== Zope/lib/python/Products/SiteErrorLog/SiteErrorLog.py 1.1.2.1 => 1.1.2.2 ===
 import os
 import sys
+import time
+from random import random
 from thread import allocate_lock
 from types import StringType, UnicodeType
 
@@ -48,7 +50,7 @@
     meta_type = 'Site Error Log'
     id = 'error_log'
 
-    keep_entries = 10
+    keep_entries = 20
     copy_to_zlog = 0
 
     security = ClassSecurityInfo()
@@ -60,6 +62,9 @@
     security.declareProtected(use_error_logging, 'getProperties')
     manage_main = PageTemplateFile('main.pt', _www)
 
+    security.declareProtected(use_error_logging, 'showEntry')
+    showEntry = PageTemplateFile('showEntry.pt', _www)
+
     security.declarePrivate('manage_beforeDelete')
     def manage_beforeDelete(self, item, container):
         if item is self:
@@ -98,33 +103,49 @@
         Called by SimpleItem's exception handler.
         """
         try:
+            now = time.time()
             try:
                 tb_text = None
                 tb_html = None
 
                 if not isinstance(info[2], StringType) and not isinstance(
                     info[2], UnicodeType):
-                    tb_text = format_exception(*info, **{'as_html': 0})
-                    tb_html = format_exception(*info, **{'as_html': 1})
+                    tb_text = ''.join(
+                        format_exception(*info, **{'as_html': 0}))
+                    tb_html = ''.join(
+                        format_exception(*info, **{'as_html': 1}))
                 else:
                     tb_text = info[2]
 
                 request = getattr(self, 'REQUEST', None)
                 url = None
+                username = None
+                req_html = None
                 if request:
                     url = request['URL']
+                    username = getSecurityManager().getUser().getUserName()
+                    try:
+                        req_html = str(request)
+                    except:
+                        pass
 
                 log = self._getLog()
                 log.append({
+                    'type': str(getattr(info[0], '__name__', info[0])),
+                    'value': str(info[1]),
+                    'time': now,
+                    'id': str(now) + str(random()), # Low chance of collision
                     'tb_text': tb_text,
                     'tb_html': tb_html,
+                    'username': username,
                     'url': url,
+                    'req_html': req_html,
                     })
 
                 cleanup_lock.acquire()
                 try:
                     if len(log) >= self.keep_entries:
-                        del log[:-keep_entries]
+                        del log[:-self.keep_entries]
                 finally:
                     cleanup_lock.release()
             except:
@@ -141,7 +162,7 @@
         return {'keep_entries': self.keep_entries,
                 'copy_to_zlog': self.copy_to_zlog}
 
-    security.declarePublic('checkEventLogPermission')
+    security.declareProtected(log_to_event_log, 'checkEventLogPermission')
     def checkEventLogPermission(self):
         if not getSecurityManager().checkPermission(log_to_event_log, self):
             raise Unauthorized, ('You do not have the "%s" permission.' %
@@ -152,18 +173,38 @@
     def setProperties(self, keep_entries, copy_to_zlog=0, RESPONSE=None):
         """Sets the properties of this site error log.
         """
-        self.keep_entries = int(keep_entries)
         copy_to_zlog = not not copy_to_zlog
         if copy_to_zlog and not self.copy_to_zlog:
             # Before turning on event logging, check the permission.
             self.checkEventLogPermission()
+        self.keep_entries = int(keep_entries)
         self.copy_to_zlog = copy_to_zlog
         if RESPONSE is not None:
             RESPONSE.redirect(
                 '%s/manage_main?manage_tabs_message=Changed+properties.' %
                 self.absolute_url())
     
-    
+    security.declareProtected(use_error_logging, 'getLogEntries')
+    def getLogEntries(self):
+        """Returns the entries in the log.
+
+        Makes a copy to prevent changes.
+        """
+        # List incomprehension ;-)
+        return [entry.copy() for entry in self._getLog()]
+
+    security.declareProtected(use_error_logging, 'getLogEntryById')
+    def getLogEntryById(self, id):
+        """Returns the specified log entry.
+
+        Makes a copy to prevent changes.  Returns None if not found.
+        """
+        for entry in self._getLog():
+            if entry['id'] == id:
+                return entry.copy()
+        return None
+
+
 
 Globals.InitializeClass(SiteErrorLog)