Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Possible scoping issue with Log::Log4perl logger ( END { undef $log; } )

by beech (Parson)
on Jun 16, 2018 at 22:31 UTC ( #1216795=note: print w/replies, xml ) Need Help??


in reply to Possible scoping issue with Log::Log4perl logger

Hi

I see things working as designed so far, as in the appender gets called

#!/usr/bin/perl -- use strict; use warnings; use Log::Log4perl 1.49; my $logger ; sub bye { $logger->info("bye"); } { my $logconfig = <<'__LOGCONFIG__'; log4perl.rootLogger = ALL, First, Mailer log4perl.appender.First = Log::Log4perl::Appender::Screen log4perl.appender.First.layout = SimpleLayout log4perl.appender.Mailer = FakeMailSend log4perl.appender.Mailer.to = FakeMailSend@example.com log4perl.appender.Mailer.subject = FakeMailSend log4perl.appender.Mailer.layout = SimpleLayout # log4perl.appender.Mailer.buffered = 0 __LOGCONFIG__ Log::Log4perl::init( \$logconfig ); $logger = Log::Log4perl->get_logger; $logger->debug('debug'); $logger->info('info'); $logger->warn('warn'); $logger->error('error'); $logger->trace('trace'); $logger->fatal('fatal'); bye; } exit( 0 ); BEGIN { package FakeMailSend; use Log::Dispatch::Email::MailSend qw[]; use base qw( Log::Dispatch::Email::MailSend ); sub send_email { warn "@_\n"; } 1; } __END__ DEBUG - debug INFO - info WARN - warn ERROR - error TRACE - trace FATAL - fatal INFO - bye FakeMailSend=HASH(0xe33e14) message DEBUG - debug INFO - info WARN - warn ERROR - error TRACE - trace FATAL - fatal INFO - bye

But if I actually try sending the mail (rename my sub send_mail)

Can't locate object method "open" via package "Mail::Mailer::smtp" at +lib/Mail/Send.pm line 58 during global destruction.

Without buffering its  Died at site/lib/Mail/Mailer.pm line 158. for every attempt.

An explicit  undef $logger ; produces the same good result

Adding  use Mail::Mailer::smtp(); produces the same failure to send the mail to example :)  No such file or directory at site/lib/Mail/Mailer.pm line 158 during global destruction.

as without buffering  No such file or directory at site/lib/Mail/Mailer.pm line 158.

Trying to  require Mail::Mailer::smtp; from within send_email fails as before  Can't locate object method "open" via package "Mail::Mailer::smtp"

So, Mail::Mailer::smtp can't be loaded during global destruction. Does Email::... eventually use it? Not important :)

So, if using a global for convenience, solution is

END { undef $logger ; }

Replies are listed 'Best First'.
Re^2: Possible scoping issue with Log::Log4perl logger ( END { undef $log; } )
by talexb (Canon) on Jun 16, 2018 at 23:35 UTC

    Thanks, the undef $logger solution occurred to me as I was running errands today .. but I'm still going to conduct some tests to see if I understand how scoping on a file variable works. I thought I understood, but apparently I have not yet reached enlightenment. :)

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1216795]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2018-11-14 13:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My code is most likely broken because:
















    Results (171 votes). Check out past polls.

    Notices?