Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Logging question

by wellsja (Novice)
on May 11, 2004 at 15:48 UTC ( #352494=perlquestion: print w/replies, xml ) Need Help??

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

Hi I have created a log4perl script that will tail a log file and will check every 10 seconds if there is a new entry in the tailed log file and then send a message accordingly. What I would like is the same , but a extra timer feature that will send an error message every 60 seconds if there is no new message in the log file. This is like an exception to state that maybe what was writing to the log file has stopped. I have pasted the code below Thanks in advance wellsja
use Log::Log4perl; use Log::Netcool::MMlogger; use IO::Select; # Use a properties file to configure the logging environment # using the configuration provided in the file .log.conf.. # Note that an XML properties file could also be used Log::Log4perl->init("sample.conf"); # # This is an Array to reference the named loggers my $log = Log::Log4perl->get_logger("LogFile"); # ############################################################## # $select = IO::Select->new(); # repeat next two lines for all filehandles to poll open(INFILE, "< file.log") || die; seek(INFILE, 0, 2); $select->add(*INFILE); for (;;) { if (@ready = $select->can_read) { # input waiting on the filehandles in @ready foreach $filehandle (@ready) { while (<$filehandle>) { chomp(); if (m/^Hello/) { $log->error("This is a hello msg"); } else { $log->error("message was not recieved"); } } } sleep 10; } }

Replies are listed 'Best First'.
Re: Logging question
by duff (Parson) on May 11, 2004 at 16:33 UTC
    In addition to what Roy Johnson said, change your call to can_read to include the timeout rather then doing a sleep at the bottom of the if block. The complete code could look something like this:

    for (;;) { if (@ready = $select->can_read(10)) { # input waiting on the filehandles in @ready foreach $filehandle (@ready) { while (<$filehandle>) { chomp(); if (m/^Hello/) { $log->error("This is a hello msg"); } else { $log->error("message was not recieved"); } } } $cycles = 0; } elsif (++$cycles % 6 == 0) { $log->error("No new messages in the last 60 seconds"); } }

      Note that having the timeout in can_read is actually essential, or else when nothing is writing to the file you'll never return from the can_read call - this means you won't generate the message unless you follow this advice and add a timeout to can_read.

      Of course, if you're already putting a timeout in the can_read call anyway, why not go all the way and just put the 60 second timeout in there? Something like this:

      for (;;) { if (@ready = $select->can_read(60)) { # input waiting on the filehandles in @ready foreach $filehandle (@ready) { while (<$filehandle>) { chomp(); if (m/^Hello/) { $log->error("This is a hello msg"); } else { $log->error("message was not recieved"); } } } } else { $log->error("No new messages in the last 60 seconds"); } }

      Now, this does mean that your "every 10 seconds" idea has gone away, but it was never really there in the first place - as I mentioned before, can_read will block forever if you don't give it a timeout, and this code won't be running your cpu time through the roof because can_read will block, up to the given timeout interval.

      As someone else mentioned, though, look at File::Tail and see if it can do the hard stuff for you.

      -- @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/
Re: Logging question
by Roy Johnson (Monsignor) on May 11, 2004 at 16:02 UTC
    Tack on an elsif clause to your main if block:
    elsif (++$cycles % 6 ==0 ) { $log->error("No new messages in the last 60 seconds"); }
    And reset $cycles to zero inside the main if block.

    The PerlMonk tr/// Advocate
Re: Logging question
by bmcatt (Friar) on May 11, 2004 at 16:36 UTC
    I'd suggest not reinventing the wheel... Take a look at File::Tail, as I believe it does most of your heavy-lifting for you.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://352494]
Approved by davido
Front-paged by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2022-05-19 22:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (72 votes). Check out past polls.

    Notices?