Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

How to read the last updated lines from a log file

by perladdict (Chaplain)
on May 30, 2013 at 04:24 UTC ( #1035955=perlquestion: print w/ replies, xml ) Need Help??
perladdict has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
I have implemented a script which sends the last modified log file via mail.But as this file is long user unable to recognize the latest files processed status in the log, They needed the last update lines from the log
Below is the code i tried to send the log.
use Posix; use Utils; $now = localtime(); %months = ("jan"=>1,.... "Dec"=>12); @now = split(" ",$now); $month $months{$now[1]}; $year = $now[-1]; $date = $now[-2]; $month = sprintf("%2d",$month); $date = sprintf("%2d",$date); $logdate = "$year$month$date"; $logpat = "xxxx"; opendir(FD,"$logpath\20130507.log") or die "$!\n"; @folders = map { [stat "$logpath/$_",$_] } grep (!/^\.\.?$/,readdir(FD +); close(FD); #fetching the last modified log sub rev_by_date { $b->[9] <=> $a->[9] } @sorted_list = sort rev_by_date @folders; @newlist = @{$sorted_list[0]}; $latestlog = pop(@newlist); $log = "$logpath\$latestlog"; open(F,"$log") or die "$!\n"; @logcontent = <F>; close(F); sendmail($now,\@logcontent); sub sendmail { my($recdate,$content) = @_; my @list = @$content; my $mail_body; my $ToEmailAdd = "xxxx.com"; my $subject = "Server archival completed"; my $from = "xxxx"; my $email_body = "some String"; $email_body .= "@list" . \n";; $osmtp = Net::SMPT->new("xxxx.com",Timeout =>60); $osmpt->mail($from); $osmpt->to($ToEmailAdd); $osmpt->date(); $osmpt->datasend("From: $from"); $osmpt->datasend("To: $TOEmailAdd"); $osmpt->datasend("Subject: $subject"); $osmpt->datasend("body: $email_body"); $osmpt->quit;

But i need to send only the last updated lines as log to the user by eamil. The log is having below tags for each didderent date. <START date= "Tue May 720:10: 32 2013"><USER></USER>/br> <FNAME>filenames</FNAME><RECNAME>recordname</RECNAME><RECLOG>logpath</RELOG>
sone lines in between....
..........
<files processed="10" recorded = "0" failed = "10" rejected = "0">
<END> date = "Tue May 720:10: 32 2013"/>
</START>
The above tags are repeated for the different dates in the log, i need to send only the last occurence of the log(latest updted content)<START>....</START> content.

Dear Monks suggest me how i need to parse the log with only last occurence of of <START>..../<START> log content
Thank you Very Much in Advance

Comment on How to read the last updated lines from a log file
Download Code
Re: How to read the last updated lines from a log file
by Anonymous Monk on May 30, 2013 at 04:39 UTC

    Well, first thing I'd do is run your program through perltidy, then rewrite it to use more subs, so that when you're stuck on an one particular part, it doesn't depend on the rest of your program ... :)

Re: How to read the last updated lines from a log file
by frozenwithjoy (Curate) on May 30, 2013 at 04:41 UTC
    Dear Monks suggest me how i need to parse the log with only last occurrence of of <START>....</START> log content
    How about just reading the log file backwards (see How do I read a file line by line in reverse order (from EOF to start of file)) and stopping when you hit the first <START> (i.e., the beginning of the last log file)? You could capture the lines (in reverse order) in your @logcontent array and then reverse the order (to the proper order) with @logcontent = reverse @logcontent before calling sendmail( $now, \@logcontent );.
      You could capture the lines (in reverse order) in your @logcontent array and then reverse the order (to the proper order) with @logcontent = reverse @logcontent ...

      Store each line read in reverse order by doing an unshift rather than a push onto the @logcontent array then there will be no need for the reverse.

      Cheers,

      JohnGG

Re: How to read the last updated lines from a log file
by poj (Priest) on May 30, 2013 at 07:30 UTC
    A simple solution is to reset a variable at each <START> tag like this. I've added a few extra print messages to show the processing ;
    #!/usr/bin/perl use strict; # use strftime use POSIX qw(strftime); my $logdate = strftime "%Y%m%d", localtime; my $logpath = "c:/temp" # change to suit; # set directory my $dir = "$logpath/$logdate"; # get list of files with mtime in seconds opendir(FD,$dir) or die "$!\n"; my @files = map { [ (stat("$dir/$_"))[9] ,$_ ] } grep !/^\.\.?$/,readd +ir(FD); print "List of files in $dir\n"; for (@files){ print "@$_\n" }; close(FD); # sort latest first sub rev_by_date { $b->[0] <=> $a->[0] }; my @sorted_list = sort rev_by_date @files; print "List of sorted files\n"; for (@sorted_list){ print "@$_\n" }; # shift latest file from front of array my $latestlog = ( shift @sorted_list )->[1]; print "lastest log = $latestlog\n"; #fetching the last modified log my $logfile = "$dir/$latestlog"; open(F,'<',$logfile) or die "Could not open $logfile : $!\n"; # store last message only my $message; while (<F>){ if (/^<START date/){ $message = $_; } else { $message .= $_; } } close(F); print $message; #sendmail($now,$message);
    poj
Re: How to read the last updated lines from a log file
by t_rex_joe (Acolyte) on May 30, 2013 at 17:20 UTC
    Look up the "use File::Tail;" Module.
    # interval = first sleep time before checking file # maxinterval = max sleep time before check the file. It is random f +rom 0 -> Max inteval # reset_tail = Action upon file being closed/reopened. 0 = Do nothin +g, -1 = Read in all file, DEFAULT = -1. $filen=File::Tail->new(name=>$if, interval=>1, maxinterval=>5, reset +_tail=>0); while(defined($line=$filen->read)) { print "$line\n"; }
    From there you can parse some time infomation with "time()" for epoch conversion in seconds. This will effectively open a "tail -f" to the file and read in the log as it's written to the file. $line is the latest line read from file. Joe
Re: How to read the last updated lines from a log file
by davido (Archbishop) on May 30, 2013 at 17:24 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1035955]
Approved by frozenwithjoy
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2014-12-25 12:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (160 votes), past polls