[Zope3-checkins] SVN: Zope3/branches/srichter-twisted-integration/src/zope/app/server/log. Now that I have submitted a patch to Twisted for a common access logger,

Stephan Richter srichter at cosmos.phy.tufts.edu
Tue Apr 26 12:44:12 EDT 2005


Log message for revision 30186:
  Now that I have submitted a patch to Twisted for a common access logger, 
  we do not have to do the work ourselves anymore.
  

Changed:
  U   Zope3/branches/srichter-twisted-integration/src/zope/app/server/log.py
  U   Zope3/branches/srichter-twisted-integration/src/zope/app/server/log.txt

-=-
Modified: Zope3/branches/srichter-twisted-integration/src/zope/app/server/log.py
===================================================================
--- Zope3/branches/srichter-twisted-integration/src/zope/app/server/log.py	2005-04-26 16:42:21 UTC (rev 30185)
+++ Zope3/branches/srichter-twisted-integration/src/zope/app/server/log.py	2005-04-26 16:44:12 UTC (rev 30186)
@@ -21,92 +21,22 @@
 __docformat__ = "reStructuredText"
 
 import logging
-import time
-import twisted.python.log
+import twisted.web2.log
 from twisted import web2
-from zope.interface import implements
-from zope.app.http.httpdate import monthname
 
 
-class CommonAccessLoggingObserver(object):
-    """Outputs accesses in common HTTP log format."""
+class CommonAccessLoggingObserver(web2.log.BaseCommonAccessLoggingObserver):
+    """Writes common access log to python's logging framework."""
 
     def __init__(self, logger=None):
         if logger is None:
             logger = logging.getLogger('accesslog')
         self.logger = logger
 
-    def computeTimezoneForLog(self, tz):
-        if tz > 0:
-            neg = 1
-        else:
-            neg = 0
-            tz = -tz
-        h, rem = divmod (tz, 3600)
-        m, rem = divmod (rem, 60)
-        if neg:
-            return '-%02d%02d' % (h, m)
-        else:
-            return '+%02d%02d' % (h, m)
+    def logMessage(self, message):
+        self.logger.log(logging.INFO, message)
 
-    tzForLog = None
-    tzForLogAlt = None
 
-    def logDateString(self, when):
-        logtime = time.localtime(when)
-        Y, M, D, h, m, s = logtime[:6]
-        
-        if not time.daylight:
-            tz = self.tzForLog
-            if tz is None:
-                tz = self.computeTimezoneForLog(time.timezone)
-                self.tzForLog = tz
-        else:
-            tz = self.tzForLogAlt
-            if tz is None:
-                tz = self.computeTimezoneForLog(time.altzone)
-                self.tzForLogAlt = tz
-
-        return '%d/%s/%02d:%02d:%02d:%02d %s' % (
-            D, monthname[M], Y, h, m, s, tz)
-
-    def emit(self, eventDict):
-        """See zope.app.logger.interfaces.IPublisherRequestLogger"""
-        if eventDict.get('interface') is not web2.iweb.IRequest:
-            return
-
-        request = eventDict['request']
-
-        firstLine = '%s %s HTTP/%s' %(
-            request.method,
-            request.uri,
-            '.'.join([str(x) for x in request.clientproto]))
-        
-        self.logger.log(logging.INFO,
-            '%s - %s [%s] "%s" %s %d "%s" "%s"' %(
-                request.chanRequest.transport.client[0],
-                request.response.headers.getRawHeaders(
-                    'x-zope-principal', ['anonymous'])[-1],
-                self.logDateString(
-                    request.response.headers.getHeader('date', time.time())),
-                firstLine,
-                request.response.code,
-                request.bytesSent,
-                request.headers.getHeader('referer', '-'),
-                request.headers.getHeader('user-agent', '-')
-                )
-            )
-
-    def start(self):
-        """Start observing log events."""
-        twisted.python.log.addObserver(self.emit)
-
-    def stop(self):
-        """Stop observing log events."""
-        twisted.python.log.removeObserver(self.emit)
-
-
-
 class CommonFTPActivityLoggingObserver(CommonAccessLoggingObserver):
     """Outputs hits in common HTTP log format."""
 

Modified: Zope3/branches/srichter-twisted-integration/src/zope/app/server/log.txt
===================================================================
--- Zope3/branches/srichter-twisted-integration/src/zope/app/server/log.txt	2005-04-26 16:42:21 UTC (rev 30185)
+++ Zope3/branches/srichter-twisted-integration/src/zope/app/server/log.txt	2005-04-26 16:44:12 UTC (rev 30186)
@@ -43,16 +43,18 @@
   ''
 
 because it is listening only to specific log dictionaries. The dictionary must
-contain an `interface` key that specifies ``web2.iweb.IRequest`` and a `request`
-key that contains the HTTP request implementing ``web2.iweb.IRequest``:
+contain an `interface` key that specifies ``web2.iweb.IRequest``, a `request`
+key that contains the HTTP request implementing ``web2.iweb.IRequest``, and
+the `response` of the request:
 
   >>> chanRequest = http.HTTPChannelRequest(None, 'GET /index.html HTTP/1.1', 1)
   >>> chanRequest.transport.client = ('127.0.0.1', 0)
   >>> request = http.Request(chanRequest, 'GET', '/index.html', (1, 1),
   ...                        http_headers.Headers())
-  >>> request.response = http.Response()
+  >>> response = http.Response()
 
-  >>> eventDict = {'interface': iweb.IRequest, 'request': request}
+  >>> eventDict = {'interface': iweb.IRequest, 
+  ...              'request': request, 'response': response}
 
 If we now emit a log event, we should receive an entry:
 



More information about the Zope3-Checkins mailing list