CGI & Python (error)

Walter Hofmann spamdropbox at myrealbox.com
Sun Mar 4 19:07:59 EST 2001


On Sun, 04 Mar 2001 18:19:20 GMT, David Fuess <fuess at att.net> 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
>yourself.

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

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.

Walter



#-------------------------------------------------------------
# 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