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

I'm seeing behavior that I can't explain when using log4perl to send a message by email.

So the following test script works just fine and an email is sent without problems:

#!/usr/bin/perl use strict; use warnings; use Log::Log4perl qw(:easy); use Log::Dispatch; my $appender_email = Log::Log4perl::Appender->new( "Log::Dispatch::Email::SSMTP", threshold => "INFO", to => 'myemail@mail.com', subject => 'Perl script message' ); my $email_logger = get_logger(); $email_logger->level($INFO); $email_logger->add_appender($appender_email); $email_logger->info('hi');

The "Log::Dispatch::Email::SSMTP" is a module I wrote to send emails using the ssmtp command.

The weirdness begins when this same exact code is moved to another package in another file in the same directory as my original script. When I do that and use that package from within my original script, no email gets sent and there are no errors thrown.

However, if I change:

Log::Dispatch::Email::SSMTP

to

Log::Log4perl::Appender::Screen

It prints out "hi" to the screen just fine when I run my script.

So if log4perl works when sending the message to the screen, why doesn't it work when trying to send an email? And why does the same code fire an email from within the original script and not from a package? Again, there are no errors getting thrown or any kind of indication that something went wrong.

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon";
$nysus = $PM . $MCF;
Click here if you love Perl Monks

Replies are listed 'Best First'.
Re: log4perl run from within another package not firing messages sent through email
by Anonymous Monk on Dec 03, 2015 at 07:49 UTC

    You have typo, this is what you posted  email_logger->level($INFO);

Re: log4perl run from within another package not firing messages sent through email
by nysus (Parson) on Dec 03, 2015 at 19:58 UTC
      Does anyone know why I needed the "buffered" property when I run this code from inside a package and not when run from within "main"?

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon";
      $nysus = $PM . $MCF;
      Click here if you love Perl Monks

        Have you tried examining your Appender object with Data::Dumper or similar to see what the value of the buffered attribute is when you create it from within your main script? At least if you knew whether it was being similarly set to '0' you could narrow your search to 'why?' ...

        The way forward always starts with a minimal test.