Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

STDERR and CGI::CARP

by Schmunzie (Beadle)
on Mar 04, 2021 at 13:15 UTC ( [id://11129117]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Wise Monks, I use CGI::Carp to view errors in the web browser during development. In production, I would like to divert STDERR to a logfile. I after bright ideas for an efficient and reliable way of switching between the two. Thanks

Replies are listed 'Best First'.
Re: STDERR and CGI::CARP
by LanX (Saint) on Mar 04, 2021 at 13:28 UTC
    If you disable CGI::Carp all messages will go to your server-logs.

    You can make this decision based on a dedicated $ENV{PRODUCTION} variable or $ENV{HOST} eq "name of production".

    See use if in use

    edit : more in pragma if

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Re: STDERR and CGI::CARP
by BillKSmith (Monsignor) on Mar 04, 2021 at 13:53 UTC
Re: STDERR and CGI::CARP
by linxdev (Sexton) on Mar 04, 2021 at 20:19 UTC
    I run into two problems using CGI. Errors during execution caused by something and errors in parsing caused by me and fat fingers. The former creates a browser on the other end that has receiv ed half of a page of HTML. The later creates a user starting at a 500 error with no context. I fixed this tow ways:
    1. I buffer all output to the browser in a string and then flush that buffer to STDOUT before exiting. If I encounter a problem during running I can output a boilerplate html page with some info.
    2. Lighttpd executes a wrapper who runs the correct file. This can be expensive with cycles, but allows me to capture errors I've created.
    In my module that manages web sessions I have a log() function that looks similar to this
    log { my @args = @_; my $fmt = shift @args; open my $fh, ">>", "/var/log/error.log" or return; printf {$fh} $fmt, @args; close $fh; return; }
    The open, append, close logic is compatible with log rotation. Anything capture on STDERR when executing the real CGI script wil be written there. The CGI script can also use the same function to write to the log file. To use the buffering idea you have to capture the output of any methods you use in CGI that want to print to STDOUT. For years, the only methods I used in the CGI module was the ones to retrieve the form values. It is possible that CGI has its own buffer with flush. I'll do something like this
    { my $buffer = ""; sub _print { my @args = @_; $buffer .= sprintf shift @args, @args; return; } sub _flush { my $content = $buffer; $buffer = ""; local $| = 1; print $content; } } _print($cgi->header( -type => .... )); ...... _flus();
Re: STDERR and CGI::CARP
by Anonymous Monk on Mar 04, 2021 at 20:13 UTC
    Also check out things like Log::Log4perl ... or the many other CPAN packages on the "Log::" branch.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11129117]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2024-04-23 18:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found