14

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.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
Freddie
  • 143
  • 1
  • 5
  • The warning you see is a compiler warning. Set the `$SIG{__WARN__}` handler in a BEGIN block: ` my $logger; BEGIN { $logger = get_logger('Foo::Bar'); $SIG{__WARN__} = sub { $logger->warn( "WARN @_" );` – daotoad Jan 17 '10 at 23:25
  • Right! Thanks everyone for the answers, using the $SIG{__WARN__} handler in a BEGIN block does the trick, catches all warnings, including compile time warnings. – Freddie Jan 18 '10 at 07:01
  • 2
    You could have accepted the answer that said that :) – brian d foy Jan 18 '10 at 17:20

2 Answers2

15

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
  • 76,436
  • 32
  • 213
  • 198
10

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
  • 129,424
  • 31
  • 207
  • 592