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

I'm using Test::Output to test the output of a module generated by Log::Log4perl. The tests work, but the output of Log::Log4perl is getting suppressed. I can use redirection to get the logs to show on the screen, but then the tests fail. I'm out of random things to try at this point and hoping some a Monk can steer me right. Here is a sample test:

stderr_like { Spin->run; } qr/Loading Spinfile from module/i, 'loads module spinfile';

The above passes. But if I change it to:

stdout_like { Spin->run; } qr/Loading Spinfile from module/i, 'loads module spinfile';
I get the following, which prints the logs, but fails the test:
# Running my tests [INFO ] No Spinfile given in command, looking in current directory. + Spin.Command: 26 [INFO ] No Spinfile found in the current directory, looking in module +share directory. Spin.Command: 33 [INFO ] Loading Spinfile from module share directory + Spin.Command: 36 not ok 1 - loads module spinfile # Failed test 'loads module spinfile' # at t/02-command.t line 31. # STDOUT: # # doesn't match: # (?^i:Loading Spinfile from module) # as expected

I've tried just about everything. The combined_like function does not help. And I've tried all kinds of different way of redirecting stderr/stdin to the point of exhaustion. I'm sure there's probably something simple I can do. Thanks.

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

Replies are listed 'Best First'.
Re: How can you get normal log output when testing log output with Test::Output?
by 1nickt (Canon) on Oct 17, 2018 at 21:38 UTC

    Hi, I don't see what is the mystery? From what you've shown, the module prints messages using Log::Log4perl to STDERR. When you test this, your test succeeds. When you look for the messages on STDOUT, they are not there, unsurprisingly. If you want to see the messages on STDOUT, you need to configure your logger to send them there.

    From Log::Log4perl::Appender::Screen:

    If stderr is set to a false value, it will log to STDOUT (or, more accurately, whichever file handle is selected via select(), STDOUT by default).
    use Log::Log4perl::Appender::Screen; my $app = Log::Log4perl::Appender::Screen->new( stderr => 0, utf8 => 1, );

    (Disclaimer: I don't use Log::Log4perl myself: I prefer Log::Any.)

    Hope this helps!


    The way forward always starts with a minimal test.

      Ah, I had the mistaken assumption the log files were printed to stdout and redirected by Test::Harness. So I thought it was some issue with the testing module.

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