2009/6/23  <pa...@compugenic.com>:
> I have a script which runs mostly via a cron job and sometimes interactively. 
>  I would like STDERR to automatically print to both the console and to a 
> logfile simultaneously.
>
> Right now I've gotten as far as merging both file handles with IO::Tee but 
> I'm not sure if I'm heading down the right path.
> my $merged_stderr = IO::Tee->new( \*STDERR, new IO::File(">>$errlog") );
>
> This only works when explicitely naming the file handle in a print statement. 
>  How can I take it to the next step, which is to have STDERR automatically 
> print to that file handle?
>
>

Hi,

What causes writting of STDERR?
If it's due to die and warn, you could redirect them to the customized routines.
Something like:


$SIG{__DIE__}=\&log_die;
$SIG{__WARN__}=\&log_warn;

sub log_die
{
    my $time=scalar localtime;
    open (HDW,">>",$err_log);
    my $old=select HDW;$|=1;select $old;
    print HDW $time,"  ",@_;
    close HDW;
    die @_;
}

sub log_warn
{
    my $time=scalar localtime;
    open (HDW,">>",$err_log);
    my $old=select HDW;$|=1;select $old;
    print HDW $time,"  ",@_;
    close HDW;
}

-- 
In this magical land, everywhere
is in full bloom with flowers of evil.
                     - Jeff Pang (CN)

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to