Custom message formatting callback for Log::Dispatch

D

Dan

I want to set up Log::Dispatch with two outputs... "file" and "email".
I'd like to use a callback function to do custom formatting of the
message based on whether the output is going to a logfile or email.

I.E. a one line entry if sent to logfile and a verbose description,
possibly including html, hyperlinks, etc if sent by email.

I figured since the log method supports sending arbitrary named pairs
to the callback function, all I needed to do was get the name of the
Dispatch object doing the logging, and have the callback do the
necessary formatting based on the destination, but getting the name of
the Dispatch object is proving to be beyond my very measly perl
programming skills!

Any help would be greatly appreciated!

Thanks,
-Dan

(e-mail address removed)
(remove reverse("nospam") to reply via email.) Spambots are getting
waaay too smart nowadays!


======================
Sample code follows:
======================

#!/usr/local/bin/perl -w

use Log::Dispatch;
use Log::Dispatch::Email::MailSend;
use Log::Dispatch::File;
use Data::Dumper;

$sub = sub { my %p = @_;
##############################################
# if $p{type} == "email" do some formatting on
$p{message};
# else do something else... or maybe nothing ;-P
##############################################
print Dumper(\%p);
return $p{message};
};

$dispatcher = Log::Dispatch->new( callbacks => $sub);

$dispatcher->add( Log::Dispatch::File->new( name => 'file1',
min_level => 'debug',
mode => 'append',
filename => 'logfile' ) );

$dispatcher->add( Log::Dispatch::Email::MailSend->new( name =>
'email',
min_level => 'error',
to => [ qw( (e-mail address removed) (e-mail address removed) ) ],
subject => 'Oh no!!!!!!!!!!!') );

#################################################################
# Need "type" to automatically reflect dispatcher->outputs->name...
#################################################################
$dispatcher->log( level => 'error', message => "ERROR level logging
test\n", type=>"???" );

print Dumper(\$dispatcher);

#EOF
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top