[Zope] Logging to a DBMS

Hannu Krosing hannu@tm.ee
Mon, 03 Apr 2000 20:37:49 +0300


Douglas Bates wrote:
> 
> I am using zope on a Debian GNU/Linux 2.2 system (the zope_2.1.4-3
> Debian package).  I notice that the access logs are written to a plain
> text file /var/lib/zope/var/Z2.log.
> 
> Are there facilities within Zope to cause it to write its logs to a
> DBMS?  I didn't see anything like this in the documentation except for
> the ability to set ZSYSLOG_SERVER which I don't really think is for
> the access logs.
>

I have hacked me a way to log info in postgres db by

1. writing a function smart_log(user,path) in a separate module 

file: PGSQLLogger.py 
-----8<-----------8<-----------8<-----------8<-----------8<------
import pg,time

def smart_log(user,path):
    f = open('/tmp/Asmartlog.txt','a')
    f.write(user+path+'\n')
    f.close()
    if path[:4] != '/pt/': return
    if user == '-': return
    log_time = '%d-%d-%d %d:%s:%d' % time.localtime(time.time())[:6]
    con = pg.connect('ptweb',user='hannu')
    path = 'http://www.puidutali.ee%s' % path
    con.query("""
        insert into
            access_log(access_when,who,what)
            values('%s','%s','%s')""" % (log_time,user,path))
    con.close()
-----8<-----------8<-----------8<-----------8<-----------8<------

and then using it from a fixed (current logging mechanisms in Zope2 don't log
users ;-O)
PCGIServer.py

fixed log_request() looks like this:
-----8<-----------8<-----------8<-----------8<-----------8<------
    def log_request(self, bytes):
        if self.env.has_key('PATH_INFO'):
            path=self.env['PATH_INFO']
        else:
            path='%s/' % self.server.module
        if self.env.has_key('REQUEST_METHOD'):
            method=self.env['REQUEST_METHOD']
        else:
            method="GET"
        if self.env.has_key('HTTP_CGI_AUTHORIZATION'):
            raw_user = self.env['HTTP_CGI_AUTHORIZATION']
            if not raw_user:
                auth_user = '-'
            else:
                auth_type, auth_string = string.split(raw_user)
                if auth_type != 'Basic':
                    auth_user = '?'
                else:
                    decoded_auth_string = base64.decodestring(auth_string)
                    auth_user = string.split(decoded_auth_string,':')[0]
        else:
            auth_user = '-'
 
        smart_log(auth_user,path)

        addr=self.addr
        if addr and type(addr) is TupleType:
            self.server.logger.log (
                addr[0],
                ' %d - %s [%s] "%s %s" %d %d' % (
                    addr[1],
                    auth_user,
                    time.strftime (
                    '%d/%b/%Y:%H:%M:%S ',
                    time.gmtime(time.time())
                    ) + tz_for_log,
                    method, path, self.reply_code, bytes
                    )
                )
        else:
            self.server.logger.log (
                '127.0.0.1',
                ' - %s [%s] "%s %s" %d %d' % (
                    auth_user,
                    time.strftime (  
                    '%d/%b/%Y:%H:%M:%S ',
                    time.gmtime(time.time())
                    ) + tz_for_log,
                    method, path, self.reply_code, bytes
                    )
                )
-----8<-----------8<-----------8<-----------8<-----------8<------

if you are using any other *Server.py-s you will have to add extracting the
user 
info yourself. Possibly you must also fix the log format as it may be broken
(as 
it was in PCGIServer.py)

------------------------
Hannu