Making Perl Warnings and Errors appear in a logfile

  Print
By default, error messages are sent to STDERR. Most HTTPD servers direct STDERR to the server's error log. Some applications may wish to keep private error logs, distinct from the server's error log, or they may wish to direct error messages to STDOUT so that the browser will receive them.

The carpout() function is provided for this purpose. Since carpout() is not exported by default, you must import it explicitly by saying:
use CGI::Carp qw(carpout);

The carpout() function requires one argument, which should be a reference to an open filehandle for writing errors. It should be called in a BEGIN block at the top of the CGI application so that compiler errors will be caught.

Example:

#!/usr/bin/perl -w

BEGIN {
use CGI::Carp qw(carpout);
open(LOG, ">>/path/to/logs/dir/mycgi.log") or
die("Unable to open mycgi.log: $!\n");
carpout(LOG);
}

Note: carpout() does not handle file locking on the log for you at this point.

The real STDERR is not closed -- it is moved to SAVEERR. Some servers, when dealing with CGI scripts, close their connection to the browser when the script closes STDOUT and STDERR. SAVEERR is used to prevent this from happening prematurely.

Making Perl Warnings and Errors appear in the browser
If you want to send fatal (die, confess) errors to the browser, ask to import the special "fatalsToBrowser" subroutine:

#!/usr/bin/perl -w

use CGI qw /:standard center/;
use CGI::Carp qw(fatalsToBrowser);

Fatal errors will now be echoed to the browser as well as to the log. CGI::Carp arranges to send a minimal HTTP header to the browser so that even errors that occur in the early compilation phase will be seen. Nonfatal errors will still be directed to the log file only (unless redirected with carpout).

Did you find this article useful?   0 out of 0 people found this article useful.

  • Comments

Add Comment

Replying to  

  Refresh
Self-Hosted Help Desk Software by SupportPal.

Login

 
Forgot password?
Register now

Language