Update: See my reply below to jasonk. I've recoded it to include chunks from the Schedule::Cron and log4perl manpages, but now it returns nothing.

I've written a script to ping servers using a Schedule::Cron job and log using Log4perl. It logs the first time the cron job runs, but thereafter, it simply logs a blank message:

2007/12/28 22:16:00 test.pl 84> INFO: this is a test 2007/12/28 22:16:00 test.pl 84> 0: 2007/12/28 22:17:00 test.pl 84> 0: 2007/12/28 22:17:00 test.pl 84> INFO: this is a test 2007/12/28 22:17:00 test.pl 84> 0: 2007/12/28 22:18:00 test.pl 84> 0: 2007/12/28 22:18:00 test.pl 84> INFO: this is a test 2007/12/28 22:18:00 test.pl 84> 0:
I know the cron job is writing correctly, as the testfile shows the correct times:
# tail /tmp/testfile Fri Dec 28 22:12:00 GMT 2007 ... Fri Dec 28 22:18:00 GMT 2007 Fri Dec 28 22:19:00 GMT 2007 Fri Dec 28 22:20:00 GMT 2007 Fri Dec 28 22:21:00 GMT 2007
I doublechecked and found no other test.pl processes running.

My guess is that it's finding null for level, converting to int, and printing it... but I don't understand why.

As I'm not sure if Schedule::Cron is passing the data correctly or whether something is amiss with Log4perl , I've included the code below.

Thoughts, anyone?

/more

#!/usr/bin/perl use Data::Dumper; use Carp; use Schedule::Cron; use Sys::Hostname; use Log::Log4perl qw(get_logger); ############################################### #Variable declarations my $rootdir = "/usr/local/appname"; my $script = "test"; ############################################### #MAIN #OpenLog ($logfile); my $cron = new Schedule::Cron(\&dispatcher, nofork => 1, log => \&Log, ); $cron->add_entry("* * * * *", {'subroutine' => \&TestSub, } ); $cron->run(detach=>1, pid_file=>"$rootdir/bin/$script.pid"); ###################################################### sub dispatcher { print "ID: ",shift,"\n"; print "Args: ","@_","\n"; } ###################################################### sub TestSub { Log ("INFO", "", "this is a test"); `date >> /tmp/testfile`; } ###################################################### sub Log { my ($level, $subject, $msg) = @_; my $conf = q( log4perl.category.appname = INFO, Logfile log4perl.appender.Logfile = Log::Log4perl::Append +er::File log4perl.appender.Logfile.filename = /usr/local/appname/lo +gs/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 = get_logger("appname"); $logger->info("$level: $msg"); if ($level eq "ERR") { $logger->error($msg); } }

-- Burvil


In reply to Log4perl script logs OK only initially by bowei_99

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.