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

UPDATE:I discovered Test::Fatal and used it like so:

like(exception{ $checker->_get_event_data('qwerty.pl') }, qr/Failed to + retrieve data from file/, 'file does not exist');

logf subroutine required following adjustment:

$logger->fatal("(Line: $line) " . $error ); print "\n"; open (STDOUT, '>/dev/null'); #squelch duplicate error output die $error;

END UPDATE

I've got a simple subroutine utility that I've been using with all my scripts that prints out the details of a script failure using the Log::Log4Perlmodule:

sub logf { my $error = shift; my $line = shift; if (!$line) { $line = [caller(0)]->[2]; } $logger->fatal("(Line: $line) " . $error); die "\n"; }

I want to test the following bit of code:

sub _get_event_data { my $self = shift; my $file = shift; my $data; eval { $data = retrieve($file); }; if ($@) { logf('Failed to retrieve data from file'); ### Line that calls fai +lure subroutine above } my $existing_events = $data->{existing_events}; return $existing_events; }

I want to test to see if the module returns the correct error messages. Here is the test:

throws_ok{ $checker->_get_event_data('qwerty.pl') } qr/Failed to retri +eve data from file/, 'file does not exist';

The test never passes, however because it has no idea what the $logger object printed. I tried changing the logf() function to:

die ($logger->fatal("(Line: $line) " .  $error));

This did not get the test to pass, however. The following worked but the test script dies and the remaining tests don't run:

$logger->fatal("(Line: $line) " . $error); die "$error\n";

So am I going to have to abandon my user of the Log4Perl module to be able to test the errors my script generates? Is there something else I might try?

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

Replies are listed 'Best First'.
Re: Testing output of logger output when dying
by stevieb (Canon) on May 20, 2016 at 15:50 UTC

    update: meh, I see you've already tried what I have below...

    update2: I can't reproduce what you're seeing (test exits):

    use Test::Exception tests => 2; use Test::More; sub blah { die "died!"; } throws_ok { blah() } qr/died!/, 'died ok'; is (1, 1, 'one is one'); __END__ ok 1 - died ok ok 2 - one is one

    /updates

    Isn't:

    $logger->fatal("(Line: $line) " . $error); die "\n";

    Simply logging a message, and then die()ing with only a newline character being set to $@, which I presume that throws_ok() will see?

    Does:

    $logger->fatal("(Line: $line) " . $error); die "(Line: $line) " . $error;

    help?

Re: Testing output of logger output when dying
by Corion (Patriarch) on May 20, 2016 at 15:33 UTC

    I don't know much about Log::Log4Perl, but can't you tell it to "log" into an array and then later in your test program inspect whether the array contains what you want?

      I was using logging to also test my script but it was unwieldy.

      I just discovered Test::Fatal which I think will help me solve this problem.

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