Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Log4perl swallowing $@

by saberworks (Curate)
on Oct 28, 2010 at 19:52 UTC ( [id://868134]=perlquestion: print w/replies, xml ) Need Help??

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

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?

Replies are listed 'Best First'.
Re: Log4perl swallowing $@
by ikegami (Patriarch) on Oct 28, 2010 at 20:03 UTC
    You're surprised that a commonly used global variable gets used? Yeah, the logger should localise it's use — file a bug report — but that's rarely done, and it's not biggy that it doesn't.
    if (my $e = $@) { print $e; $logger->debug('test'); print $e; print 'after printing $e'; }
      You're right, it should be obvious that any external bit of code could use $@, thanks for that. I'm so used to seeing if($@) { # handle error } that I didn't really think about it properly.
        Oh I don't know about that. Actually, I think you *are* thinking about it properly. Although it makes sense that external code will refer to certain variables, it is natural to expect that said code won't walk all over those variables without restoring them or at least telling you about it in some way. File that report, in my opinion.
Re: Log4perl swallowing $@
by ELISHEVA (Prior) on Oct 29, 2010 at 02:34 UTC

    -- I'm so used to seeing if($@) { # handle error } --

    Unless this is throwaway code, in the meantime until that bug is fixed, it wouldn't hurt to add a comment to your code to explain why you needed to do $e=$@, just so that someone further down the pike doesn't think that your own assignment code is redundant or unnecessarily confusing and try to "clean" it up.

    If you are so used to seeing if ($@)..., the same is likely true for others.

Re: Log4perl swallowing $@
by saintmike (Vicar) on Dec 26, 2011 at 04:38 UTC
    Sorry, I'm late to the party, but just for the record: Log4perl isn't resetting $@ here, it's a 3rd party appender from CPAN called Log::Dispatch::FileRotate. (You can easily verify this by replacing it by Log4perl's native file appender Log::Log4perl::Appender::File).

    Might be a good idea to file a bug against it in order to have it localize its use of $@, although, as others have pointed out, it's a common mistake.

Re: Log4perl swallowing $@
by Anonymous Monk on Oct 29, 2010 at 06:57 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://868134]
Approved by ikegami
Front-paged by ig
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-04-18 13:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found