|There's more than one way to do things|
Possible scoping issue with Log::Log4perl loggerby talexb (Canon)
|on Jun 15, 2018 at 21:52 UTC||Need Help??|
talexb has asked for the wisdom of the Perl Monks concerning the following question:
I hope this isn't a stupid question, but at least I've discovered the solution to my problem, and I'm happy to share this story in the event that it helps someone else.
I've developed a pile of scripts that use Log::Log4perl, a module that I love to bits. Wanting to implement a system that would E-Mail me when there was a problem, I decided to add an E-Mail appender to my configuration. It worked fine in my test scripts, but didn't work in my work scripts, and I couldn't understand why.
I finally figured it out this afternoon (which will explain why I'm pausing to sip from my tall can of Waterloo Amber "Ontario's First Craft Brewery"). My test script looks like this:
This works fine. My work scripts are bigger and have subroutines in which I want to do logging, so they look like this:
See the difference? I'm using what I call a file scoped variable in the work script so that I don't have to pass the logger into the subroutine (something MSCHILLI goes 'Brrrr!' to here). I believe Log::Log4perl collects log entries to go out by E-Mail until the logger goes out of scope.
So the test script works fine. The work script's logger (I thought) would also go out of scope and send the E-Mail when the script finishes -- but no E-Mail is sent. Why not?
Anyway, my workaround is going to be to call get_logger in whichever scope I need to do logging. This works fine, but I'm left wondering why doesn't Log::Log4perl see that the logger has gone out of scope in the work script and send the E-Mail? Is there a 'finalize' or 'terminate' method I could have called to trigger this? Is this an error in the behaviour? (I find that hard to believe, for a module that's been around since 2002).