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?
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|