http://qs321.pair.com?node_id=793264

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

I'm trying to extend Log::Log4perl so that I can trap all messages for a process and, at the end, format them all nicely in an email. The example I have below only deals with error messages to more simply demonstrate my issue.

I'm using perl 5.8.2 and Log::Log4perl 1.22

Here is my logger package:

package Mylogger; use warnings; use strict; use base qw(Log::Log4perl); my @error_msgs; sub init { Log::Log4perl::init(@_); return; } sub get_logger { $Log::Log4perl::caller_depth = 1; my $logger = Log::Log4perl::get_logger(@_); bless $logger, __PACKAGE__; return $logger; } sub error { my ($this, $msg) = @_; push(@error_msgs, "ERROR $msg"); $this->SUPER::error($msg); return; } sub get_logged_errors { return \@error_msgs; } 1;
Here is my test program:
use strict; use warnings; use FindBin; use Mylogger; my $conf = q( log4perl.rootLogger=DEBUG, Screen log4perl.appender.Screen = Log::Log4perl::Appender::Screen log4perl.appender.Screen.stderr = 0 log4perl.appender.Screen.layout=PatternLayout log4perl.appender.Screen.layout.ConversionPattern=%d{ISO8601} %-5p + %C %m%n ); Mylogger::init(\$conf); my $log = Mylogger::get_logger(); $log->error('error 1'); $log->error('error 2');
Here is a run of my test program showing two errors:
#test.pl Can't locate object method "error" via package "Mylogger" at Mylogger. +pm line 25. Can't locate object method "DESTROY" via package "Mylogger" at /usr/op +t/perl5/lib/site_perl/5.8.2/Log/Log4perl/Logger.pm line 60. END failed--call queue aborted.
The error on line 25 in MyLogger is the call the SUPER::error, which somehow cannot be found. Also there is some issue with the way I'm implementing this which causes Log4perl to not find a DESTROY method. What am I missing here?