John Dunbar wrote:
> Now: system "command 2>>errors.log";
> I would like to redirect that error output to logger.

Well, the safest and most direct way would be to replace/augment all the
prints to STDERR (like "warn" and "die") inside your script with
log4perl logger calls.

Or you could pipe the output of your script to another script that reads
STDIN and implements logging like this:

 command 2>&1 | perl -e 'use Log::Log4perl qw/:easy/; \
 Log::Log4perl::easy_init($INFO);  \
 while (<STDIN>){INFO $_}'

Or, if you wanted to get really crazy, if your perl is 5.8.0 or better,
you can use PerlIO::via to grab STDERR and redirect it to your logger.
This is completely untested, and I've never done it myself, but it would
look something like this:
---------------------------------------------
use PerlIO::via::Logger;

use Log::Log4perl;
Log::Log4perl->init(\<<EOL);
log4perl.logger = DEBUG, FileAppndr1

log4perl.appender.FileAppndr1      = Log::Log4perl::Appender::File
log4perl.appender.FileAppndr1.filename = stderr.log
log4perl.appender.FileAppndr1.layout = Log::Log4perl::Layout::SimpleLayout
EOL

close STDERR;
open(STDERR, ">:via(Logger)", '/dev/null');

warn "this is a warning, but stderr is being handled by the logger";
---------------------------------------------
package PerlIO::via::Logger;

use Log::Log4perl qw/:easy/;

sub PUSHED {
     my ($class,$mode,$fh) = @_;
     my $buf = '';
     if ($mode ne 'w'){
        #gah, can't use warn or die if they've closed STDERR!
        print STDOUT "this logger is only usable for writing, mode
'$mode' not allowed";
        exit(1);
    }
     return bless \$buf,$class;
}


sub WRITE {
     my ($obj,$buf,$fh) = @_;
     ERROR("message on stderr: $buf");
     return length($buf);
}

sub FLUSH {
        my ($obj,$fh) = @_;
        #ignoring flush
        return 0;
}

1;
---------------------------------------


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
log4perl-devel mailing list
log4perl-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/log4perl-devel

Reply via email to