|
From: Chris B. <ch...@ge...> - 2007-05-23 12:29:42
|
Hi,
I've been working an a little web application, and needed to write a new
appender. I don't know if there is already a way to do something like
this. Anyway I thought I'd share it.
Basically the rationale was that part of the code can either be run in a
production environment and log any errors to a database for later use,
or it can be run in 'test mode' from an administration interface, where
debugging messages are passed back to the user in a process report.
The usage is outlined in the Synopsis below, and it seams to work quite
well. Any comments or suggestions would be greatly appreciated.
Also, I would like to publish this on my "please give me a better job"
website. Does anyone object to this (esp. Mike Schilli and Kevin Goess -
have I given you appropriate attribution?)
cheers
--chris
package Log::Log4perl::Appender::Array;
our @ISA = qw(Log::Log4perl::Appender);
use Data::Dumper;
##################################################
# Log dispatcher writing to an arrayref
##################################################
##################################################
sub new {
##################################################
my $proto = shift;
my $class = ref $proto || $proto;
my %params = @_;
my $self = {
name => "Array Dispatcher",
array=> [],
params => \%params,
};
bless $self, $class;
}
##################################################
sub log {
##################################################
my $self = shift;
my %params = @_;
my $newhash = {};
foreach my $key (keys %{$self->{params}{keys}}) {
if ($self->{params}{keys}{$key} =~ /^VAL_(\d)/) {
$newhash->{$key} = $params{message}[$1-1];
} else {
my $conv = Log::Log4perl::Layout::PatternLayout->new(
{
ConversionPattern => {
value => $self->{params}{keys}{$key}
}
}
);
$newhash->{$key} =$conv->render(
$params{message},
$params{log4p_category},
$params{log4p_level},
5 + $Log::Log4perl::caller_depth,
);
}
}
push (@{$self->{array}}, {params => \%params, values => $newhash });
}
##################################################
sub array {
##################################################
my($self, $level) = @_;
if ($level) {
return [
map { $_->{values} }
grep { $_->{params}{level} > ($level - 1) }
@{$self->{array}}
];
} else {
return [
map { $_->{values} }
@{$self->{array}}
];
}
}
##################################################
sub reset {
##################################################
my ($self) = @_;
$self->{array} = [];
}
1;
__END__
=head1 NAME
Log::Log4perl::Appender::Array - Append to aan Array
=head1 SYNOPSIS
my $config = <<'EOT';
log4perl.rootLogger=DEBUG, DebugArray
log4perl.appender.DebugArray = Log::Log4perl::Appender::Array
#Set up the keys for the hash
#Use VAL_1, VAL_2, etc for the values passed to the log
statement
log4perl.appender.DebugArray.keys.timestamp = %d
log4perl.appender.DebugArray.keys.category = %c
log4perl.appender.DebugArray.keys.priority = %p
log4perl.appender.DebugArray.keys.customer = VAL_1
log4perl.appender.DebugArray.keys.message = VAL_2
#just pass through the array of message items in the log
statement
log4perl.appender.DebugArray.layout =
Log::Log4perl::Layout::NoopLayout
log4perl.appender.DebugArray.warp_message = 0
# Append to the array
$logger->warn( $custid, 'big problem!!');
# Retrieve the result
my $appender = Log::Log4perl::appender_by_name('DebugArray');
my $result = $appender->array($level); #$level is optional - if
omitted the entire array is returned
# Reset the buffer
$appender->reset();
=head1 DESCRIPTION
This is a simple appender based heavily on the DBI Appender by Kevin
Goess and the String appender by Mike Schilli.
It's designed to work in conjunction with the DBI appender, such that
the same modules can log to the Array appender in
a test environment and the debugging values can be returned directly to
the user without being permanently recorded, while
in a production environment error logs can be recorded in a database (or
to the file system).
=head1 SEE ALSO
=head1 AUTHOR
Chris Baxter <E<lt>cp...@ge...<gt>
=cut
|