http://www.perlmonks.org?node_id=436661

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

For a while now, we have been testing a script that runs several cluster applications. We found the error output went only to the screen of shell where it was run. making it difficult for multiple to work together to locate the problem.

Directing STDERR to a file helped, but it was hard to figure out where in the script the error occurred. I found this code works best by redirecting STDOUT and STDERR both to the log file. It seems to work well, but we've only started using it. Do you see any problems or ways to improve this?

use strict; # Set up main log open LOG, ">$logfile" || die "$0: Can't open log file!"; open ( STDERR, ">>$logfile" ); open ( STDOUT, ">>$logfile" ); select( LOG ); $| = 1; # Turn on buffer autoflush for log output select( STDOUT );

Replies are listed 'Best First'.
Re: Redirecting logging and STDERR to a single file
by Limbic~Region (Chancellor) on Mar 04, 2005 at 17:40 UTC
Re: Redirecting logging and STDERR to a single file
by moot (Chaplain) on Mar 04, 2005 at 17:50 UTC
    Generally I've found logging direct to a file very limiting (unless the app is running as a daemon and has no terminal). If run from the shell I prefer to redirect using the shell:

    ./some_script > /some/log
    For directing both STDOUT and STDERR you could use this (bash):
    ./some_script >& /some/log
    or this (shells that don't support >&):
    ./some_script > /some/log 2>&1
    making sure to have turned on $| in your script of course.

    That way you can see output on the terminal if you wish; filter it; send stdout to /dev/null; or just send all output to the log directly - some of which are difficult to do if your script itself is writing direct to the log.

Re: Redirecting logging and STDERR to a single file
by johnnywang (Priest) on Mar 04, 2005 at 23:41 UTC
    try Log::Log4perl, it will give you everything you need about logging: logging to multiple places (screen, file, syslog, database, etc.), file & line, etc.
Re: Redirecting logging and STDERR to a single file
by brian_d_foy (Abbot) on Mar 04, 2005 at 21:37 UTC

    If you have trouble telling where an error occured in the script, output file and line information along with your error message. If this thing is spread across multiple machines, you could even add host info to each line of error output.

    I generally have a subroutine that handles that sort of thing for me. That way, it's behaviour is in one place, and the rest of the script only has to know that it is logging something (and not the details of whre it goes or what other information is attached).

    --
    brian d foy <bdfoy@cpan.org>