Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Getting "close() on unopened filehandle FH" from Log::Log4perl

by glasswalk3r (Friar)
on Jul 31, 2013 at 15:23 UTC ( #1047283=perlquestion: print w/replies, xml ) Need Help??
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


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> 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/ 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/ 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

Replies are listed 'Best First'.
Re: Getting "close() on unopened filehandle FH" from Log::Log4perl
by kcott (Chancellor) on Jul 31, 2013 at 23:35 UTC

    G'day glasswalk3r,

    I see two issues here:

    1. In run(), you have "__PACKAGE->gimme_logger()". That should probably be "__PACKAGE__->gimme_logger()". [You may be across this information already but, in case you're not: __PACKAGE__ is described in "Special Literals" under perldata - Scalar value constructors; and Log::Log4perl - Pitfalls with Categories describes a caveat when using __PACKAGE__.]
    2. gimme_logger(), run() and DEMOLISH() all have "my $self = shift;" but none of them use $self.

    -- Ken

      Thanks Ken,

      I have already got the typo with __PACKAGE__, I'll update the node.

      Regarding the $self variable, gimme_logger() really does not need it, I'll update it as well. The other does use $self, but I haven't put all the code

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1047283]
Approved by marto
Front-paged by sparkyichi
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (2)
As of 2018-05-23 00:03 GMT
Find Nodes?
    Voting Booth?