Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Help on capturing only latest entries or lines from a logfile(Since last scan/read) on windows

by crazy-duck (Novice)
on Jul 09, 2015 at 23:40 UTC ( #1134058=perlquestion: print w/replies, xml ) Need Help??
crazy-duck has asked for the wisdom of the Perl Monks concerning the following question:

Help on capturing only latest entries or lines from a logfile(Since last scan/read) on windows

We have plain text file which gets appended with latest events if any specific to application on a windows server, We check the application log file every 1 hour, it will be great help if there is any sample perl script which can achieve this.All we want to do is read the new events and write it to a new logfile.

  • Comment on Help on capturing only latest entries or lines from a logfile(Since last scan/read) on windows

Replies are listed 'Best First'.
Re: Help on capturing only latest entries or lines from a logfile(Since last scan/read) on windows
by afoken (Abbot) on Jul 10, 2015 at 10:16 UTC

    Save the log file size to a helper file when your log reader script ends. When the log reader script starts again, read the old log file size back, then open the log file and seek to the old file size.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: Help on capturing only latest entries or lines from a logfile(Since last scan/read) on windows
by i5513 (Pilgrim) on Jul 10, 2015 at 11:51 UTC
    Maybe you can be interested on porting logtail2 to windows (or finding an similar stuff on windows)
    If you install coreutils on windows, you could use "tac file | grep -m 5 xxx" to see the last 5 times where xxx appeared on the file
    Regards
Re: Help on capturing only latest events from a logfile on windows
by Anonymous Monk on Jul 09, 2015 at 23:55 UTC
Re: Help on capturing only latest entries or lines from a logfile(Since last scan/read) on windows
by sundialsvc4 (Abbot) on Jul 10, 2015 at 12:05 UTC

    This is relatively easy to do:   seek() can position the cursor to a point “relative to the end of the file,” so seek to a position slightly before where you know the last n lines are likely to be.   (Logfile records are often of equal size; certainly, there is a maximum.)   Now, start reading lines sequentially.   The first line read is almost certain to be truncated, so just throw it away.   Read the remaining lines into an array, and “slice” that array to keep only the last n lines.

      It would be easier to keep track of what size the file was the last time it was read, then seek that far from the beginning. afoken already mentioned that solution.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1134058]
Front-paged by Corion
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2018-07-21 19:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (450 votes). Check out past polls.

    Notices?