use Log::Log4perl qw/:easy :no_extra_logdie_message/;
$SIG{__DIE__} = sub {
return if $^S; # ignore die in an eval block
# Get the actual caller for the "die" and not the wrapper
local $Log::Log4perl::caller_depth;
$Log::Log4perl::caller_depth++;
LOGDIE($_[0]);
};
####
perl -v
This is perl 5, version 16, subversion 3 (v5.16.3) built for MSWin32-x86-multi-thread
(with 1 registered patch, see perl -V for more detail)
Copyright 1987-2012, Larry Wall
Binary build 1603 [296746] provided by ActiveState http://www.ActiveState.com
Built Mar 13 2013 11:29:21
####
$SIG{__DIE__} = sub {
my ($message) = @_;
chomp $message;
# Get the actual caller for the "die" and not the wrapper
local $Log::Log4perl::caller_depth;
$Log::Log4perl::caller_depth++;
my $log = get_logger('');
$log->logdie($message);
};
####
# the new $SIG{__DIE__} causes everything to die here:
eval{ do_something(\@args) or die "'do_something()' failed: $!"; };
# This never gets reached
if ( $@ ) {
warn "Could not do_something(): [$@]";
something_else();
}
####
use YAML qw/LoadFile/;
# dies with the following error:
# Can't locate Mo/builder.pm in @INC (@INC contains: C:/Perl/site/lib C:/Perl/lib .) at (eval 65) line 2, <$IN> line 1.
my $yaml = LoadFile('./config.yml');
####
$SIG{__DIE__} = sub {
return if $^S; # ignore die in an eval block
...
};
####
#!/usr/bin/perl
use Log::Log4perl qw/get_logger :no_extra_logdie_message/;
use strict;
use warnings;
use YAML qw/LoadFile/;
CHECK {
LOG_INIT: {
# Initialize logging
local $/ = undef;
Log::Log4perl::init(\);
}
LOG_WRAPPERS: {
# Wrapper for logging fatal errors
$SIG{__DIE__} = sub {
#return if $^S; # ignore die in an eval block
my ($message) = @_;
chomp $message;
# Get the actual caller for the "die" and not the wrapper
local $Log::Log4perl::caller_depth;
$Log::Log4perl::caller_depth++;
my $log = get_logger('');
$log->logdie($message);
};
}
}
####################
## MAIN
{
#Can't locate Mo/builder.pm in @INC (@INC contains: C:/Perl/site/lib C:/Perl/lib .) at (eval 65) line 2, <$IN> line 1.
my $yaml = LoadFile('./path/to/yaml/file.yml') or die "Could not parse config file:\n$!\n$^E";
eval { die "a hot potato"; }; # dies here
if ($@) { print "I caught something:\n[$@]"; exit; } # this is never reached
}
__DATA__
layout_class = Log::Log4perl::Layout::PatternLayout
# Layout for screen logging
# [Priority] [File::Caller] [Line Number] > Message
debug_layout_pattern = [%p] [%F{1}::%M] [%L] > %m %n
##
## LOGGERS
##
log4perl.logger = TRACE, LogScreen
# Logging to screen
# (Includes TRACE and DEBUG priority level messages)
log4perl.appender.LogScreen = Log::Log4perl::Appender::Screen
log4perl.appender.LogScreen.utf8 = 1
log4perl.appender.LogScreen.layout = ${layout_class}
log4perl.appender.LogScreen.layout.ConversionPattern = ${debug_layout_pattern}