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

Hi All,
  When testing my CGI scripts on a local machine, anything printed to STDERR seems to end up in my browser. Such as output from tests ran with Test::Harness::Straps.
I've also had this happen with some client machines (Fedora I think), but on others the STDERR doesn't end up in the browser, I think it goes straight to the error log.

I've no idea how to control where the STDERR info goes to. In some situations, I wan't it to go to the browser (such as tests with Test::Harness::Straps), other times I want it to go to the error log.

I this something that is set and controled somehow with Perl? Or is it a setting on the webserver (IIS and Apache)?

Thanks for any pointers


Lyle
  • Comment on Output of STDERR - Probably an easy one

Replies are listed 'Best First'.
Re: Output of STDERR - Probably an easy one
by McDarren (Abbot) on Oct 26, 2008 at 14:15 UTC
    See CGI::Carp
    It includes methods such as fatalsToBrowser and warningsToBrowser that are useful for debugging and controlling which messages go to the browser.

    Cheers,
    Darren

Re: Output of STDERR - Probably an easy one
by ccn (Vicar) on Oct 26, 2008 at 14:10 UTC

    You can use Log::Log4perl. It is very powerful module for logging

    You can log to STDOUT or to file or to both of them simultaneously. It's really handy, must have tool.

Re: Output of STDERR - Probably an easy one
by moritz (Cardinal) on Oct 26, 2008 at 14:14 UTC
Re: Output of STDERR - Probably an easy one
by ig (Vicar) on Oct 26, 2008 at 18:56 UTC

    Note also that RFC-3875 The Common Gateway Interface (CGI) Version 1.1 says that the web server will execute the CGI script in a "system-defined manner". While it does say that the "system-defined method" for returning responses will be by writing to the "standard output" unless defined otherwise, it says nothing about standard error output (STDERR) as a channel for communication between the CGI script and the web server. If you are writing your scripts to be portable, you might consider it best to avoid any output to STDERR, returning any error messages as well formatted CGI responses or writing them to application specific log files.

Re: Output of STDERR - Probably an easy one
by ig (Vicar) on Oct 26, 2008 at 18:35 UTC
    I this something that is set and controled somehow with Perl? Or is it a setting on the webserver (IIS and Apache)?

    If your script does nothing with STDERR other than write to it, then what happens will be determined by the webserver. What the webserver does by default and how to configure it depends on your webserver. Consider that you may be running IIS, Apache or some other server. With IIS your script may be run by perl.exe as a CGI script or by perlis.dll as an ISAPI plugin. With Apache, your script may be run as a CGI script or you may be running mod_perl, fastCGI or some other context. You will have to investigate the specifics of your servers to learn the defaults and options.

    But, your script can do more than just write to STDERR. It can close STDERR as provided by the server and re-open it connected to a file or process of your chosing. So the script can also determine what happens with data written to STDERR.

    And, many error messages can be diverted to other destinations before they are written to STDERR (e.g. use CGI::Carp qw(fatalsToBrowser)). Other responses provide several good alternatives for redirection of error messages within your scripts.

Re: Output of STDERR - Probably an easy one
by Anonymous Monk on Oct 26, 2008 at 14:15 UTC
    Neither Apache nor IIS will send STDERR to STDOUT, so it must be your code, something like
    use CGI::Carp qw(fatalsToBrowser); CGI::Carp::carpout(\*STDOUT);
      I've done some further testing and IIS definitely sends STDERR to STDOUT (for perl.exe through CGI at least).

      Although thanks to your code I can now get Linux/Apache to do the same :)