CGI & Python (error)

Walter Hofmann spamdropbox at
Mon Mar 5 01:07:59 CET 2001

On Sun, 04 Mar 2001 18:19:20 GMT, David Fuess <fuess at> wrote:
>Debugging CGI can be a trying task. You might want to enclose the
>entire CGI in a try-except structure so you can trap all errors and
>return a reasonable result to the server and write an error log for

I once wrote the attached module for debugging purposes. Start your CGI

import log
log.init ("/path/to/logfile", "nameofprogram")

and all error messages should end up in the log file, unless you have a
syntax error in the main module. You can also use the error, warning and
info functiond to log things.


# This file contains the logging functions

import sys, time, string

def error (message):
    file.write (time.asctime (time.gmtime (time.time ()))+' UTC [error] '+name+': '+message+'\n')
    file.flush ()

def warning (message):
    file.write (time.asctime (time.gmtime (time.time ()))+' UTC [warning] '+name+': '+message+'\n')
    file.flush ()

def info (message):
    file.write (time.asctime (time.gmtime (time.time ()))+' UTC [info] '+name+': '+message+'\n')
    file.flush ()

class logredirect:
    def __init__ (self):
        self.saved = ""
    def write (self, message):
        list = string.split (self.saved+message, "\n")
        self.saved = list[-1]
        del list[-1]
        for line in list:
            error (line)

def init (logfile, myname):
    global file, name
    file = open (logfile, "a")
    name = myname
    sys.stderr = logredirect()

More information about the Python-list mailing list