Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Redirecting logging and STDERR to a single file

by bwelch (Curate)
on Mar 04, 2005 at 17:34 UTC ( #436661=perlquestion: print w/replies, xml ) Need Help??

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 <>

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://436661]
Approved by Joost
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2023-12-02 06:18 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (13 votes). Check out past polls.