Jump to content

Getting Last 5 Lines of server logs


sesk
 Share

Recommended Posts

Hello, I am working on a script to handle my server console and manage logs, I got it working ok but the log part isn't functional.

I'm looking for a way to get the last 5 lines of a *.log file (5-20mb file) and store them in a common *.log file, with time and date title, like:

---12:30:21 PM - 3/20/2007 ---
Last Line -4
Last Line -3
Last Line -2
Last Line -1
Last Line

And how to make it so it doesn't procced to the next operation until the lines are read and stored, since when the server starts, it wipes the log files.

Thanks for reading.

Link to comment
Share on other sites

Hello, I am working on a script to handle my server console and manage logs, I got it working ok but the log part isn't functional.

I'm looking for a way to get the last 5 lines of a *.log file (5-20mb file) and store them in a common *.log file, with time and date title, like:

---12:30:21 PM - 3/20/2007 ---
Last Line -4
Last Line -3
Last Line -2
Last Line -1
Last Line

And how to make it so it doesn't procced to the next operation until the lines are read and stored, since when the server starts, it wipes the log files.

Thanks for reading.

you could do a FileReadToArray() then save FileWriteLine() $array[$array[0]-4] through $array[$array[0]] to another file...
Link to comment
Share on other sites

Wouldn't that be... a pretty damn big array ? File Size range is more like 5-200mb; 2,360,000 lines, avg 1million lines, and there would also be to consider the time of execution, isnt there a way to get the file line count, and like

GetLastLine(...)

do

FileLineRead(...,...,lastline)

lastline = lastline -1;

c = c +1

until c = 5;

?

Link to comment
Share on other sites

Wouldn't that be... a pretty damn big array ? File Size range is more like 5-200mb; 2,360,000 lines, avg 1million lines, and there would also be to consider the time of execution, isnt there a way to get the file line count, and like

GetLastLine(...)

do

FileLineRead(...,...,lastline)

lastline = lastline -1;

c = c +1

until c = 5;

?

you're there and could test it for time to execute etc couldn't you? and which is it, 5 - 20 mb, or 5-200? pretty big difference...
Link to comment
Share on other sites

and which is it, 5 - 20 mb, or 5-200? pretty big difference...

5-200mb , wont let me edit my original post.

Randall,

I'll try your tail thing.

EDIT: But how do you use it, i tried the ReadTail3.au3 and it only displayed the replacement time in the console :S

EDIT2: Nevermind the last line WAS " " (nothing), but now i have to figure out how to make it read the previous lines (btw nice script, real fast :whistle:)

Edited by sesk
Link to comment
Share on other sites

I think the most logical way to achieve this is to do:

#include <File.au3>
Local $file = "test.txt", $data = ""
$lines = _FileCountLines($file)
For $i = $lines-4 To $lines
    If NOT FileReadLine($file, $i) = "" Then $data = $data & FileReadLine($file, $i) & @CRLF
Next
MsgBox(0,"",StringTrimRight($data,1))

Kurt

EDIT: Improved script.

Edited by _Kurt

Awaiting Diablo III..

Link to comment
Share on other sites

$lines = _FileCountLines($file)

You still are having to read the whole file to get the "tail" end.

Find a tail binary, and use it. I have pasted below the info to help you find one.

C:\boz\bat>tail --version

tail (textutils) 2.1

Written by Paul Rubin, David MacKenzie, Ian Lance Taylor, and Jim Meyering.

Copyright © 2002 Free Software Foundation, Inc.

This is free software; see the source for copying conditions. There is NO

warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

A wrapper around tail, and all your problems are solved. Fast access at the 'last' bit of a file.

Reading the help file before you post... Not only will it make you look smarter, it will make you smarter.

Link to comment
Share on other sites

Hi,

[1]

tail (textutils) 2.1

A wrapper around tail

[2]

To get started you need to download it here : MS LogParser 2.2

(Don' t forget to register the LogParser DLL.)

Why bother with either of those options when you can use a simple AutoIt UDF [see my post above about TailRW], which is probably faster anyway?

Best, randall

Edited by randallc
Link to comment
Share on other sites

Randallc, I am using your TailRW script, but I get AutoIt error when it runs (Memory Error)

local $c_bulk = "Location of bulk txt file"
local $c_logs = "Location of server log file (txt, can't read log files...)
local $i_line = 0;
...
if ... then
        $i_line = -6;
    FileWriteLine ($c_bulk, "" )
    FileWriteLine ($c_bulk, "--- "& @MDAY &"/"& @MON &"/"& @YEAR &" -- "& @HOUR &":"& @MIN &":"& @SEC &" ---")
    FileWriteLine ($c_bulk, "" )
    do
        $i_Line = $i_Line + 1
        $s_ReadLine = __FileReadLine ($c_logs, $i_Line, 1);** READS from the TAIL if Negative line number
        FileWriteLine ($c_bulk, $s_ReadLine )
    Until $i_Line = -1
Link to comment
Share on other sites

Hi,

You have not named your file to read?

;ReadLine4.au3
#include "TailRW.au3"
$s_Test = FileOpenDialog("Choose File", @ScriptDir, "Files (*.exe;*.dll;*.txt)", 1); ** put your file name HERE
Local $c_bulk = "Location of bulk txt file" ;$s_Test must be a file name ******************
Local $c_logs = "Location of server log file (txt, can't read log files...)";$s_Test must be a file name ******************
Local $i_line = 6;
$s_ReadLine = __FileReadLine ($s_Test, -$i_line, 1, 1) ;;** READS from the TAIL if Negative line number [to the end if 4th parm=1]
;~  __FileReadLine($s_File, $i_LineNumber = -1, $i_Beginning = 1,$i_ToEnd=0, $i_BufferSize = 8388608)
;~ MsgBox(0, "", "$s_ReadLine=" & $s_ReadLine)
FileWrite("AnswerLines.txt",$s_ReadLine)
runwait ("notepad "&"AnswerLines.txt")
Best, Randall
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...