in reply to Log4perl script logs OK only initially

Your log method expects three arguments, but you are passing it as a log argument to Schedule::Cron, which is only going to call it with two arguments, and the first one will be either 0, 1, or 2 depending on the level of the message, so when you call Log directly in your TestSub, it works as expected, but when Schedule::Cron calls it, you are only logging the first argument, which is 0, and ignoring the second one (which you assigned to $subject, but then didn't do anything with). The Schedule::Cron documentation includes an example of how to use Log::Log4perl as a logger.


We're not surrounded, we're in a target-rich environment!
  • Comment on Re: Log4perl script logs OK only initially

Replies are listed 'Best First'.
Re^2: Log4perl script logs OK only initially
by bowei_99 (Friar) on Dec 30, 2007 at 05:54 UTC
    OK, so I take the example from the Schedule::Cron manpage and modify it with config settings as in the Log4perl manpage, as below, and it logs nothing at all, and the script dies.
    my $log_method = sub { my ($level,$msg) = @_; my $DBG_MAP = { 0 => $INFO, 1 => $WARN, 2 => $ERROR }; my $conf = q( log4perl.category.appname = INFO, Logfile log4perl.appender.Logfile = Log::Log4perl::Ap +pender::File log4perl.appender.Logfile.filename = /usr/local/eFTP/l +ogs/appname.pl.log log4perl.appender.Logfile.layout = \ Log::Log4perl::Layout::PatternLayout log4perl.appender.Logfile.layout.ConversionPattern = % +d %F{1} %L> %m %n ); Log::Log4perl::init(\$conf); my $logger = Log::Log4Perl->get_logger("appname"); $logger->log($DBG_MAP->{$level},$msg); };
    and modify the cron job to reflect that:
    my $cron = new Schedule::Cron(\&dispatcher, nofork => 1, #log => \&Log, log => $log_method, ); $cron->add_entry("* * * * *", {'subroutine' => \&TestSub(), 'arguments' => [ "INFO", "a test" ], } );
    I also removed the call to my Log(..) subroutine in TestSub, since it's redefined and used here in $log_method.

    Again, now there's nothing logged to the file at all. The file /tmp/testfile is updated, so I know TestSub ran, but nothing updated in appname.pl.log, and no Schedule::Cron process in the process table after a couple minutes.

    Is there something I'm not setting the Schedule::Cron object? What else could be causing not to log at all, much less quit?

    -- Burvil

      I got this working by doing the following: 1. specifying the arguments in the add_entry line, e.g.
      $cron->add_entry("0 5 * * *", {'subroutine' => \&TestSub, 'arguments' => [ ], } );
      2. Reverting back to a "Log" subroutine, since for some reason, the $log_method wasn't working for me.

      -- Burvil