Skip to content

Commit

Permalink
Now that I have submitted a patch to Twisted for a common access logger,
Browse files Browse the repository at this point in the history
we do not have to do the work ourselves anymore.
  • Loading branch information
strichter committed Apr 26, 2005
1 parent acf3ca8 commit f211550
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 79 deletions.
80 changes: 5 additions & 75 deletions log.py
Expand Up @@ -21,90 +21,20 @@
__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)

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)

def logMessage(self, message):
self.logger.log(logging.INFO, message)


class CommonFTPActivityLoggingObserver(CommonAccessLoggingObserver):
Expand Down
10 changes: 6 additions & 4 deletions log.txt
Expand Up @@ -43,16 +43,18 @@ When the system emits an arbitrary log request, the observer does nothing
''

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:

Expand Down

0 comments on commit f211550

Please sign in to comment.