Skip to content

Commit

Permalink
Reimplemented common access log using Twisted's log framework. It use…
Browse files Browse the repository at this point in the history
…s our

setup accesslog. Tests will follow, but BjornT wants to have a look.
  • Loading branch information
strichter committed Apr 20, 2005
1 parent bb2fd75 commit 0b1793c
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 3 deletions.
121 changes: 121 additions & 0 deletions log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
##############################################################################
#
# Copyright (c) 2005 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.
#
##############################################################################
"""Logging Support
Logging Support for the Twisted logging framework. A special observer will
forward messages to the standard Python Logging Framework.
$Id$
"""
__docformat__ = "reStructuredText"

import logging
import time
import twisted.python.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."""

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(response.headers.getHeader('date', 0)),
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."""

def log(self, request):
"""See zope.app.logger.interfaces.IPublisherRequestLogger"""
now = time.time()
message = ' - %s [%s] "%s %s"' % (task.channel.username,
self.log_date_string(now),
task.m_name[4:].upper(),
task.channel.cwd,
)

self.output.logRequest(task.channel.addr[0], message)
5 changes: 5 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import zope.app.appsetup
import zope.app.appsetup.interfaces
from zope.app import wsgi
from zope.app.server import log

CONFIG_FILENAME = "zope.conf"

Expand Down Expand Up @@ -105,6 +106,10 @@ def setup(options):
options.eventlog()
options.accesslog()

# Setup the logs. Eventually this might be better done using utilities.
observer = log.CommonAccessLoggingObserver()
observer.start()

zope.app.appsetup.config(options.site_definition)

db = options.database.open()
Expand Down
6 changes: 3 additions & 3 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
"""
import logging

from twisted.application import internet
from twisted.internet import reactor, interfaces

from zope.interface import implements
from zope.app import zapi
from zope.app.server.interfaces import IServerType, ISSLServerType

from twisted.application import internet
from twisted.internet import reactor, interfaces

class SSLNotSupported(Exception):
''' '''

Expand Down

0 comments on commit 0b1793c

Please sign in to comment.