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?
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.