I have been beating my head against this for a bit and I feel like I'm fundamentally misunderstanding how eval {} and $@ work. Given the following script with the following following log4perl configuration, I would expect that $@ wouldn't be unset in the middle of the if($@) block. What I'm seeing is that if I call any loglevel method on the $logger object it clears $@. Is this expected behavior?
#!perl
use warnings;
use strict;
use Log::Log4perl;
Log::Log4perl->init_and_watch('yourlogger.conf', 10);
my $logger = Log::Log4perl::get_logger('test_die');
$logger->warn("before eval");
eval {
die 'foo';
};
if($@) {
print $@; # I expect this to print "foo", it does
$logger->debug('test');
print $@; # I expect this to print "foo", it doesn't!
print 'after printing $@';
}
print "\n";
yourlogger.conf:
log4perl.rootLogger = DEBUG, stdout, file
log4perl.appender.stdout = Log::Log4perl::Appender::Screen
log4perl.appender.stdout.layout = Log::Log4perl::Layout::SimpleLayout
log4perl.appender.stdout.stderr = 1
log4perl.appender.stdout.Threshold = DEBUG
log4perl.appender.file = Log::Dispatch::FileRotate
log4perl.appender.file.mode = append
log4perl.appender.file.size = 10_000_000
log4perl.appender.file.max = 10
log4perl.appender.file.filename = foo.txt
log4perl.appender.file.layout=PatternLayout
log4perl.appender.file.layout.ConversionPattern=%H:%P (%c) %p - %d: {%
+X{work}} %m%n
What am I doing wrong?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.