tsam has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I am creating a web based application (CGI) in perl in an Unix server. To catch errors I am using a '-w' and also 'use CGI::Carp qw/fatalsToBrowser/;' which successfully throws syntax errors in the browser. But sometimes, if there is an unpredictable error I am getting a blank browser. E.g. I have kept provision for catching error if DB connection fails. But if table definitions get changed (for which I dunno how to catch the error!!) I get a blank screen. Is there any way I can write a log file that logs the error where the code is failing? Thanx, Tamojit

Replies are listed 'Best First'.
Re: How to create an error log file?
by tlm (Prior) on Apr 07, 2005 at 09:47 UTC

    This is what I use:

    BEGIN { require 5.004; use CGI::Carp qw(fatalsToBrowser carpout); my $logfile = 'path/to/logfile'; open LOG, ">>$logfile" or die "Couldn't append to $logfile: $!\n"; carpout(\*LOG); };
    This appends to the logfile all the output sent to STDERR, both during compile-time and during run-time. E.g., if I have the line
    warn "Logging...\n";
    in my code, this will send the following to the logfile:
    [Thu Apr 7 05:39:57 2005] testcgi: Logging...

    the lowliest monk

      Hi Pustular, The code works nicely....thanx a lot:)
Re: How to create an error log file?
by rev_1318 (Chaplain) on Apr 07, 2005 at 09:35 UTC
    Have you checked the logfile of your web server?

    Paul

      Hi Paul, Actually I am quite a newbie and I am not sure how to check that...
        Ask your sysadmin the location of the webserver logfiles. It might be in your homedir somewhere. The filename is usually something like error_log so if you run find ~ -name error_log on a unix/linux box, it might find the file for you.
Re: How to create an error log file?
by reasonablekeith (Deacon) on Apr 07, 2005 at 09:47 UTC
    Also, info on catching DBI errors from the DBI documentation. The important thing to note being that you can die in an eval block, but your perl script won't die.
    perldoc -m DBI perldoc -f eval
    Typically RaiseError is used in conjunction with eval { ... } to catch the exception that's been thrown and followed by an if ($@) { ... } block to handle the caught exception. For example:
    eval { ... $sth->execute(); ... }; if ($@) { # $sth->err and $DBI::err will be true if error was from DBI warn $@; # print the error ... # do whatever you need to deal with the error }
Re: How to create an error log file?
by chimni (Pilgrim) on Apr 07, 2005 at 09:36 UTC
    You can have a look at the Log4perl module. The documentation is quite good .Simple to implement regards, chimni
Re: How to create an error log file?
by loris (Hermit) on Apr 07, 2005 at 13:07 UTC

    Like chimni, I would also go for Log::Log4perl. I have found it extremely useful and easy to use. I wouldn't think of writing any moderately-sized program without it.

    loris

Re: How to create an error log file?
by pearlie (Sexton) on Apr 07, 2005 at 11:42 UTC
    Hello, On linux, the apache error log is found in dir /var/log/httpd/. Also, log::log4perl gives great flexibility to trap all errors that you want and put them in a customized format in a log file.
      On linux, the apache error log is found in dir /var/log/httpd/.

      I'm using Linux (Slackware) and the (main) error log is in /var/log/apache2/error_log.

      The sysadmin can put that directory where ever he/she wants.

      On the other hand, foreach site I always create a dedicated /logs dir under the site directory (not a public directory). This way I don't fill up the main error_log and makes it easier to follow errors on the web site I'm currently working.

      I do this when I configure a new site on Apache:

      <VirtualHost ....> ...... ErrorLog /path/to/website/error_log .... </VirtualHost>