http://www.perlmonks.org?node_id=1108270

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

I have a bunch of utility scripts I'd like to test in part by scraping their log output. The scripts use Log::Dispatch with the default screen output and a file output for logging. Presently, the tests use Test::Trap to check log output on STDOUT.

This approach works well when proving tests directly from the command line. However, we run automated tests nightly via cron and under these conditions either Dispatch::Log doesn't write log entries (likely because STDOUT goes nowhere) or Test::Trap doesn't get them. I see the same results when running the tests with nohup prove ... --html

I'd rather not completely change the test approach (to reading the file, for example, instead of using Test::Trap) just because the logging tests fail under cron. On the other hand, we rely on the nightly test output to alert us to trouble and don't want a bunch of "known" failing results.

Supposing I can detect when running under cron (probably with ENV injection in the cron script that launches the tests--I've found checking PS1 is not sufficient to know) is there some UNIX or Perl hack I can use to persuade Log::Dispatch to log such that Test::Trap gets the lines?

Replies are listed 'Best First'.
Re: Test::Trap + Log::Dispatch under Cron
by kevbot (Priest) on Nov 24, 2014 at 23:14 UTC
    I really don't have any experience using these modules, but I took a quick look at the documentation for Log::Dispatch::Screen. The screen output seems to default to writing to STDERR. If your Test::Trap code is checking STDOUT, then perhaps that's why you are not getting any output. However, I'm not sure why you would notice this problem only when running under cron.

      Well, the screen output is definitely writing to STDOUT, at least according to Test::Trap.

      I've taken an approach similar to Test::Log::Dispatch and added a code output that appends all messages to an array. I'm not getting the logs with Test::Trap anymore, but it's not a complete change to the rest of the testing approach by needing to read a temp logfile.

      our @log; $logger->add( Log::Dispatch::Code->new( min_level => 'debug', code => +sub { my %entry = @_; push @log, $entry{message}; }};