in reply to Easy, elemental, but cool debug trick.

I use a similiar technique. When debugging a CGI application, however, prints can really mess you up.

For that situation, I have a routine (below) that writes to a file. It accepts two parameters, one which is a class string, and a message string. The section string allows me to put messages in a arbitrary category (say, 'debug', 'authentication', or 'SQL'), and the text to be printed.

Each message is timestamped, and also stamped with a user ID (that's internal to me application, and could be ignored).

I also have a similiar version that use the syslog facility to write /var/log/messages.

sub debug_log { @_ == 2 or confess "Incorrect number of arguments"; my ($section, $action) = @_; my $userid = 0; my $string = 0; $userid = $globalUserID || "<nobody>"; $string = sprintf ("%s %s %s:%s\n", scalar localtime, $userid, $sec +tion, $action); open (SINK, ">> /home/httpd/somedir/logfile") or confess "Can't ope +n logfile"; print SINK $string; close (SINK) or confess "Can't close logfile"; }

Replies are listed 'Best First'.
RE: RE: Easy, elemental, but cool debug trick.
by ask (Pilgrim) on May 25, 2000 at 13:43 UTC
    Just always use warn "foobar" if $DEBUG instead of print, then it will do the right thingtm under both mod_cgi and mod_perl. For logging you should take a look at the Log::Agent module available on CPAN.