[Zope] logging users

flynt flynt@gmx.ch
Sun, 02 Dec 2001 05:11:31 +0100


Erik Myllymaki wrote:
> 
> Any easy way to add AUTHENTICATED_USER to Z2.log?

Hi Erik

Two questions: What is the Zope version you use ? In Zope 2.4.3 the
authenticated users are logged in the Z2.log. For Zope 2.3.x you have to
patch http_server.py
Second question is: what do you use for authentication ? I don't have
the answer for CookieCrumbler, but only for basic http authentication
(which is used for the basic user folder coming with Zope).

Here is the answer for basic http authentication and Zope 2.3.x (I
looked it up somewhere, but I do not remember anymore where and couldn't
find it easily again):

Go to the file *Zserver/medusa/http_server.py* and edit the method
*log*. It must read like this (don't forget the import of base64
module)::

      ...
      # python modules
      import base64
      import os
      import regex
      import re
      import socket
      import stat
      import string
      import sys
      import time

      # async modules
      ...
      ...
      def log (self, bytes):
        user_agent=self.get_header('user-agent')
        if not user_agent: user_agent=''
        referer=self.get_header('referer')
        if not referer: referer=''
        auth=self.get_header('AUTHORIZATION')
        name='Anonymous'
        if auth is not None:
                if string.lower(auth[:5]) == 'basic':
                        [name,password] = string.split(
                           base64.decodestring(
                           string.split(auth)[-1]), ':')

        self.channel.server.logger.log (
            self.channel.addr[0],
            ' - %s - [%s] "%s" %d %d "%s" "%s"\n' % (
        #       self.channel.addr[1],
                name,
                self.log_date_string (time.time()),
                self.request,
                self.reply_code,
                bytes,
                referer,
                user_agent
                )
            )
      ...

Maybe this helps.

--- Flynt