1

I have switched my loggin to use Log::Log4perl and Im having a hard time using Log::Dispatch::Email::MailSender for emailing logs. The code snippet below is my working example.

use Log::Log4perl qw(get_logger :levels);

my $log4perl_conf=<<CFG
log4perl.rootLogger=INFO, Email
log4perl.appender.Email=Log::Dispatch::Email::MailSender
log4perl.appender.Email.to=me\@me.com
log4perl.appender.Email.from=me\@me.com
log4perl.appender.Email.smtp=smtp.me.com
log4perl.appender.Email.min_level=info
log4perl.appender.Email.layout=Log::Log4perl::Layout::PatternLayout
log4perl.appender.Email.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:s
+s} [%F{1}:%c{1}] %5p> %m%n
CFG
;

Log::Log4perl::init(\$log4perl_conf);
my $logger = Log::Log4perl->get_logger();

$logger->info('The first message bla bla');
$logger->info('The second message bla bla');
some_func();

sub some_func {
    my $logger = Log::Log4perl->get_logger();
    $logger->info("some_func was called");

}

Here are my questions:

  1. why does the logger not send emails unless I add get a logger locally by adding my $logger = Log::Log4perl->get_logger(); to some_func Im not sure why it cant use the global version
  2. How do I get access to the logger configuration so I can change the email subject during execution say to indicate success or failure. I basically want to change things like subject or to address dynamically in my script

As a workaround for Q2 above in my main program I now use Log::Log4perl::Appender::String to store the message and then ues send an email at the end of my script using MIME::Lite. I would prefer to have this all handled by the logger. Thanks

melpomene
  • 84,125
  • 8
  • 85
  • 148

1 Answers1

1

Not quite understand your question #1, but for question #2: you may add this line in the log4perl.properties:

log4perl.appender.Email.subject = sub { return getEmailSubject(); }

then add a subroutine in your code:

sub getEmailSubject {   
    return "whatever I want";
}
David Zhao
  • 4,284
  • 11
  • 46
  • 60