http://www.perlmonks.org?node_id=1047283

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

Greetings monks

UPDATE:I have create a new attribute to the Daemon class to hold the logger reference from the gimme_logger sub. After that, I recovered the logger from the object instance attribute, but when reaching the DEMOLISH method, the logger attribute was set automatically to undef, finishing the process without any warning (but without the log messages as well).

I'm using Log::Log4perl to log messages for two Moose classes that I have.

These two classes have each other their own appender:

log4perl.logger.Siebel.Srvrmgr.Daemon=DEBUG, A1 log4perl.appender.A1=Log::Log4perl::Appender::File log4perl.appender.A1.filename=C:/Temp/perl/project.log log4perl.appender.A1.stderr=0 log4perl.appender.A1.layout=Log::Log4perl::Layout::PatternLayout log4perl.appender.A1.layout.ConversionPattern=%d %p> %F{1}:%L %M - %m% +n log4perl.appender.A2= Log::Log4perl::Appender::Screen log4perl.appender.A2.stderr=0 log4perl.appender.A2.layout=Log::Log4perl::Layout::PatternLayout log4perl.appender.A2.layout.ConversionPattern=%d %p> %F{1}:%L %M - %m% +n log4perl.logger.Siebel.Srvrmgr.ListParser=WARN, A2

Only the Daemon uses a file as appender.

I've created the following class method to retrieve a single instance of the logger:

sub gimme_logger { my $cfg = Siebel::Srvrmgr->logging_cfg(); die "Could not start logging facilities" unless ( Log::Log4perl->init_once( \$cfg ) ); return Log::Log4perl->get_logger('Siebel::Srvrmgr::Daemon'); }

That's how I'm invoking the logger inside each method:

sub run { my $self = shift; my $logger = __PACKAGE__->gimme_logger(); weaken($logger); # rest of code goes here

and

sub DEMOLISH { my $self = shift; my $logger = __PACKAGE__->gimme_logger(); weaken($logger); # rest of code goes here

And this is the method invoked to retrieve the loggers configuration:

sub logging_cfg { my $cfg = undef; local $/; if ( $ENV{SIEBEL_SRVRMGR_DEBUG} ) { if ( ( -e $ENV{SIEBEL_SRVRMGR_DEBUG} ) and ( -f $ENV{SIEBEL_SRVRMGR_DEBUG} ) ) { open( my $in, '<', $ENV{SIEBEL_SRVRMGR_DEBUG} ) or die "Cannot read $ENV{SIEBEL_SRVRMGR_DEBUG}: $!"; $cfg = <$in>; close($in); } else { die 'ENV SIEBEL_SRVRMGR_DEBUG is defined but the value does not exists in +the filesystem or is not a file'; } } else { $cfg = <Siebel::Srvrmgr::DATA>; } return $cfg; }

When I terminated the application, I'm getting the following messages in the CMD.exe prompt:

C:\Temp\memcached>server2.pl Starting Nagios::Cache::Server...done Starting RPC::XML::Server...done Ready to accept requests close() on unopened filehandle FH at C:/strawberry/perl/site/lib/Log/L +og4perl/Appender/File.pm line 257 during global destruction. Can't close C:/Temp/perl/project.log (Bad file descriptor) at C:/straw +berry/perl/site/lib/Log/Log4perl/Appender/File.pm line 282 during glo +bal destruction.

I'm having trouble finding out why Log::Log4perl is trying to close the appender twice, since:

  1. I'm using a single instance of logger
  2. The instance should be eliminated at the end of the method due weaken from Scalar::Utils
  3. Only the Daemon logger is using the file appender
  4. I'm not passing references of the logger outside the methods scope

Initially I was using a global scalar (declared with our) to have a single logger for the whole package, but when reaching the DEMOLISH method the object reference was not available anymore.

I'm using Strawberry perl 5.16.3 with Log::Log4perl version 1.42 in a Windows 7 box.

Alceu Rodrigues de Freitas Junior
---------------------------------
"You have enemies? Good. That means you've stood up for something, sometime in your life." - Sir Winston Churchill