cah has asked for the wisdom of the Perl Monks concerning the following question:

All, I'm attempting to capture all warnings to a log using Log::Log4perl. However, I'm unable to capture "Illegal division by zero" warning. Looking for thoughts or suggestions.
use strict; use warnings; use FindBin; use lib "$FindBin::Bin/../lib"; use Log::Log4perl qw(get_logger); # Initialize logging behaviour BEGIN { my $conf = q( log4perl.logger = DEBUG, FileApp log4perl.appender.FileApp = Log::Log4perl::Ap +pender::File log4perl.appender.FileApp.filename = test.log log4perl.appender.FileApp.layout = PatternLayout ); Log::Log4perl->init( \$conf ); $SIG{__WARN__} = sub { my $logger = get_logger(); local $Log::Log4perl::caller_depth = $Log::Log4perl::caller_dep +th + 1; $logger->warn("WARN @_"); }; } my $logger = get_logger(); $logger->error("Oh my, an error!"); my $foo = 100; my $foo = 44; my $bar = 0; 1/0;
Update, I didn't realize divide by zero caused a die... Adding the following code resolved my issue:
$SIG{__DIE__} = sub { my $logger = get_logger(); local $Log::Log4perl::caller_depth = $Log::Log4perl::caller_dep +th + 1; $logger->fatal("DIE @_"); };

Replies are listed 'Best First'.
Re: log::log4perl capture warnings
by jeffa (Bishop) on Apr 29, 2015 at 18:32 UTC

    You can't catch an error like that. Take a look at Surviving 'Illegal division by zero' for alternatives. (I think you'll need to eval a block and essentially handle this like you would catching exceptions -- that is, good old try ... catch ... throw.)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      Thanks for input... I was hoping to add logging to a number of poorly written scripts that I've inherited. Then, start working the log file to resolve the issues.
Re: log::log4perl capture warnings
by Laurent_R (Canon) on Apr 29, 2015 at 20:16 UTC
    Please note that "Illegal division by zero" is not a warning but an exception, i.e. a fatal error. With a warning, the script will usually print the warning and continue to execute. Not so with "Illegal division by zero": the program will immediately cease to execute (unless you trap the exception). Consider this one-liner:
    $ perl -e 'use strict; use warnings; my $c = 1/0; print "we get there\ +n";' Illegal division by zero at -e line 1. $
    As you can see, the final statement is not executed, we don't even "get there".

    Compare to this, where the error is trapped within an eval block:

    $ perl -e 'use strict; use warnings; eval {my $c = 1/0;}; print "we +get there but got the following problem:\n $@ \n";' we get there but got the following problem: Illegal division by zero at -e line 1. $
    Here, the program does not die, but checking the value of the $@ makes it possible for the developer to possibly take other actions than just dying (for example if something can be recovered, so that the error should not be fatal).

    Je suis Charlie.
Re: log::log4perl capture warnings
by edimusrex (Monk) on Apr 29, 2015 at 18:28 UTC
    I'm curios about that last line of code Perl is interpreting it as (1 divided by 0) which would throw and error.