tags:

views:

388

answers:

2

Log4perl is a great tool for logging.

The warnings pragma is also an essential tool.

However, when Perl scripts are running as daemons, the Perl warnings, are printed into STDERR where nobody can see them, and not into the Log4perl log file of the relevant program.

Is there a way to catch Perl warnings into the Log4perl log?

For example, this code will log just fine to the log file, but in case this is run as a daemon, the Perl warnings will be not be included in the log:

#!/usr/bin/env perl
use strict;
use warnings;

use Log::Log4perl qw(get_logger);

# Define configuration
my $conf = q(
                log4perl.logger                    = DEBUG, FileApp
                log4perl.appender.FileApp          = Log::Log4perl::Appender::File
                log4perl.appender.FileApp.filename = test.log
                log4perl.appender.FileApp.layout   = PatternLayout
);

# Initialize logging behaviour
Log::Log4perl->init( \$conf );

# Obtain a logger instance
my $logger = get_logger("Foo::Bar");
$logger->error("Oh my, an error!");

$SIG{__WARN__} = sub {
    #local $Log::Log4perl::caller_depth = $Log::Log4perl::caller_depth + 1;
    $logger->warn("WARN @_");
};

my $foo = 100;
my $foo = 44;

This still prints out to STDERR:

"my" variable $foo masks earlier declaration in same scope at log.pl line 27.

And the log file does not catch this warning.

+10  A: 

You could install a WARN handler to do this. It's mentioned in the Log4perl FAQ.

My program already uses warn() and die(). How can I switch to Log4perl?

If your program already uses Perl's warn() function to spew out error messages and you'd like to channel those into the Log4perl world, just define a WARN handler where your program or module resides:

use Log::Log4perl qw(:easy);
$SIG{__WARN__} = sub {
    local $Log::Log4perl::caller_depth =
        $Log::Log4perl::caller_depth + 1;
    WARN @_;
};

This will capture any explicit use of warn in your program, as well as Perlish warnings about use of uninitialized values and the like.

martin clayton
+4  A: 

It's in the Log4perl FAQ as Some module prints messages to STDERR. How can I funnel them to Log::Log4perl? and My program already uses warn() and die(). How can I switch to Log4perl?.

Your specific problem has the added wrinkle that you are seeing a compile-time warning, so you need to adjust the FAQ advice to setup the logging at compile time as early as possible. Do that in a BEGIN block as close to the top of the source as you can stand:

 BEGIN {
     ... all of your logging setup
     }

You can tell if it's a compile-time warning by running a syntax check with warnings enabled:

 % perl -cw program

If you see the warning during the syntax check, it's a compile-time warning.

I'd rather catch the compile-time warnings in development though. They shouldn't make it through to a production system. :)

brian d foy
I've tried these examples in the FAQ, but they don't seem to catch Perl warnings, only explicit warnings in the program using WARN
Freddie
You have to start the logging before the warnings start showing up :)
brian d foy