Tail a log file

Discuss RoboTask here
Post Reply
renders
Posts: 10
Joined: Tue Oct 02, 2018 7:47 pm

Tail a log file

Post by renders »

Hi all.. I need to pull the last 10 lines from a text log file to be sent within an email..

Suggestions?

Thanks
Oleg
Site Admin
Posts: 2773
Joined: Thu Jan 01, 1970 1:00 am
Contact:

Re: Tail a log file

Post by Oleg »

Look at my example
The task counts lines and extracts last 10 lines from text
Save the text of the task to a file and use menu Task->Import to import the task into RoboTask.
Also you can simply copy task text and paste it into the task list.
Look at the step #4. It calculates the start line in VB script

Code: Select all

;*****************************
;* RoboTask Task file
;* Do not edit in text editor!
;*****************************
 
[Root]
ActionAfterRun=INTEGER|0
Actions=FOLDER
Automat=INTEGER|-1
CatID=INTEGER|444033236
Comment=STRINGLIST
ContinueOnError=INTEGER|0
DisableOnError=INTEGER|0
DoNotStopWhenShutdown=INTEGER|0
ExternalName=STRING|"Task310"
Hide=INTEGER|0
ID=INTEGER|-589362537
LogOnAsUser=INTEGER|1
Name=STRING|"Text tail (last 10 lines)"
OnErrorTaskID=INTEGER|-1
Priority=INTEGER|3
RunOnClose=INTEGER|0
RunOnStartup=INTEGER|0
ToLog=INTEGER|3
UnicodeFormat=INTEGER|1
WriteGeneralLog=INTEGER|0

[Actions]
Action1=FOLDER
Action10=FOLDER
Action11=FOLDER
Action2=FOLDER
Action3=FOLDER
Action4=FOLDER
Action5=FOLDER
Action6=FOLDER
Action7=FOLDER
Action8=FOLDER
Action9=FOLDER

[Actions\Action1]
ActionID=STRING|"A_VARIABLES_SET"
Enabled=INTEGER|-1
Name=STRING|"Set variable ""FileName"" with value ""C:\temp\test.txt"""
Params=FOLDER

[Actions\Action1\Params]
expand=STRING|"0"
varname=STRING|"FileName"
varvalue=STRING|"C:\temp\test.txt"

[Actions\Action10]
ActionID=STRING|"A_LOOP_END"
Enabled=INTEGER|-1
Name=STRING|"End Loop"

[Actions\Action11]
ActionID=STRING|"A_DIALOG_SHOWTEXT"
Enabled=INTEGER|-1
Name=STRING|"Show Text"
Params=FOLDER

[Actions\Action11\Params]
autoclose=STRING|"0"
fromfile=STRING|"1"
line00000000=STRING|"{Txt}"
linecount=STRING|"1"
timeout=STRING|"10"
title=STRING|"Text tail"

[Actions\Action2]
ActionID=STRING|"A_TXT_NUMBEROFLINES"
Enabled=INTEGER|-1
Name=STRING|"TXT Number of Lines"
Params=FOLDER

[Actions\Action2\Params]
external=STRING|"1"
filename=STRING|"{FileName}"
linecount=STRING|"0"
variable=STRING|"LineCount"

[Actions\Action3]
ActionID=STRING|"A_ROBOTASK_LOG"
Enabled=INTEGER|-1
Name=STRING|"Log Message"
Params=FOLDER

[Actions\Action3\Params]
message=STRING|"{LineCount}"
type=STRING|"3"

[Actions\Action4]
ActionID=STRING|"A_SCRIPT_VBEVALUATE"
Enabled=INTEGER|-1
Name=STRING|"VB Evaluate"
Params=FOLDER

[Actions\Action4\Params]
expression=STRING|"GetStartLine({LineCount}, 10)"
line00000000=STRING|"function GetStartLine(cnt, delta)"
line00000001=STRING|"  start = cnt - delta + 1"
line00000002=STRING|"  if start < 1 then"
line00000003=STRING|"    start = 1"
line00000004=STRING|"  end if"
line00000005=STRING|"  GetStartLine = start"
line00000006=STRING|"end Function"
line00000008=STRING|"'logmessage(GetStartLine(10, 15))"
linecount=STRING|"9"
loadfromfile=STRING|"1"
variable=STRING|"StartLine"

[Actions\Action5]
ActionID=STRING|"A_ROBOTASK_LOG"
Enabled=INTEGER|-1
Name=STRING|"Log Message"
Params=FOLDER

[Actions\Action5\Params]
message=STRING|"{StartLine}"
type=STRING|"3"

[Actions\Action6]
ActionID=STRING|"A_VARIABLES_SET"
Enabled=INTEGER|-1
Name=STRING|"Set variable ""txt"" with value """""
Params=FOLDER

[Actions\Action6\Params]
expand=STRING|"0"
varname=STRING|"txt"

[Actions\Action7]
ActionID=STRING|"A_LOOP_SIMPLE"
Enabled=INTEGER|-1
Name=STRING|"Simple Loop"
Params=FOLDER

[Actions\Action7\Params]
begin=STRING|"{StartLine}"
end=STRING|"{LineCount}"
variable=STRING|"NUM"

[Actions\Action8]
ActionID=STRING|"A_TXT_EXTRACTLINE"
Enabled=INTEGER|-1
Name=STRING|"TXT Extract Line"
Params=FOLDER

[Actions\Action8\Params]
external=STRING|"1"
filename=STRING|"{FileName}"
linecount=STRING|"0"
linenum=STRING|"{num}"
variable=STRING|"Line"

[Actions\Action9]
ActionID=STRING|"A_VARIABLES_SET"
Enabled=INTEGER|-1
Name=STRING|"Set variable ""txt"" with value ""{txt}{Line}{Eol}"""
Params=FOLDER

[Actions\Action9\Params]
expand=STRING|"1"
varname=STRING|"txt"
varvalue=STRING|"{txt}{Line}{Eol}"

Oleg Yershov
renders
Posts: 10
Joined: Tue Oct 02, 2018 7:47 pm

Re: Tail a log file

Post by renders »

Thanks Oleg.. Worked very well for most time..

However, I have found that occasionally so many items are being written to the log around the same time that 10 lines back doesn't not capture the event that I need to display. I have increased the count to 20 but then too much info is being shown.

Can you suggest some way to filter the text so that it only shows the items around the most recent line that contains the word 'failed'

Here is a sample log.. The line in question is at 14:01:22. Ideally, the tail would be filtered for 'failed' and then display 5 lines before and after. OR, at least with the same time stamp as the error line.

Any help is much appreciated.

Best regards,
Rob

14:01:04 Directory Monitor: Searching 'C:\AudioDownload\'
14:01:04 Directory Monitor: 1 files detected
14:01:04 Importing file: 'C:\AudioDownload\SHOGB1A.wav'
14:01:04 Import: Direct category & ID import for 'SHOGB1A'
14:01:04 Active set Thu Jun 23 00:00:00 2022
14:01:08 Import: Time adjust 'SHOGB1A' 17542ms now 17541ms
14:01:08 Import: CART timing info added to 'SHOGB1A'
14:01:08 Import Successful: File 'C:\AudioDownload\SHOGB1A.wav' imported as 'SHOGB1A'
14:01:12 Directory Monitor: Searching 'C:\AudioDownload\'
14:01:12 Directory Monitor: 1 files detected
14:01:12 Importing file: 'C:\AudioDownload\SHOGB1B.wav'
14:01:12 Import: Direct category & ID import for 'SHOGB1B'
14:01:12 Active set Thu Jun 23 00:00:00 2022
14:01:15 Import: Time adjust 'SHOGB1B' 96536ms now 96535ms
14:01:15 Import: CART timing info added to 'SHOGB1B'
14:01:15 Import Successful: File 'C:\AudioDownload\SHOGB1B.wav' imported as 'SHOGB1B'
14:01:16 Directory Monitor: Searching 'C:\AudioDownload\'
14:01:16 Directory Monitor: 1 files detected
14:01:16 Importing file: 'C:\AudioDownload\SHOGB1C.wav'
14:01:16 Import: Direct category & ID import for 'SHOGB1C'
14:01:16 Active set Thu Jun 23 00:00:00 2022
14:01:22 Import: CART timing info added to 'SHOGB1C'
14:01:22 Import Successful: File 'C:\AudioDownload\SHOGB1C.wav' imported as 'SHOGB1C'
14:01:22 Directory Monitor: Searching '\\nas-1\AudioBus\!_MTS_IMPORT\WINGHAM\'
14:01:22 Directory Monitor: 2 files detected
14:01:22 Importing file: '\\nas-1\AudioBus\!_MTS_IMPORT\WINGHAM\BLACKBURN DIGITAL-CEEK JUNE 24 PODCAST PROMO.wav'
14:01:22 Import: Invalid category PRO, import failed
14:01:22 Import failed: No default category for '\\nas-1\AudioBus\!_MTS_IMPORT\WINGHAM\BLACKBURN DIGITAL-CEEK JUNE 24 PODCAST PROMO.wav'
14:01:23 Importing file: '\\nas-1\AudioBus\!_MTS_IMPORT\WINGHAM\BLACKBURN DIGITAL-CEEK JUNE 24 PODCAST.wav'
14:01:23 Import: Direct category & ID import for 'COMCEEK'
14:01:23 Active set Fri Jun 24 00:00:00 2022
14:01:23 Expiry set Fri Jul 08 00:00:00 2022
14:01:28 Import: Time adjust 'COMCEEK' 30473ms now 30472ms
14:01:28 Import: CART timing info added to 'COMCEEK'
14:01:28 Import Successful: File '\\nas-1\AudioBus\!_MTS_IMPORT\WINGHAM\BLACKBURN DIGITAL-CEEK JUNE 24 PODCAST.wav' imported as 'COMCEEK'
14:01:28 Directory Monitor: Searching 'C:\AudioDownload\'
14:01:28 Directory Monitor: 2 files detected
14:01:28 Importing file: 'C:\AudioDownload\SHOGB1D.wav'
14:01:28 Import: Direct category & ID import for 'SHOGB1D'
14:01:28 Active set Thu Jun 23 00:00:00 2022
14:01:30 Import: Time adjust 'SHOGB1D' 11246ms now 11245ms
14:01:30 Import: CART timing info added to 'SHOGB1D'
14:01:30 Import Successful: File 'C:\AudioDownload\SHOGB1D.wav' imported as 'SHOGB1D'
14:01:30 Importing file: 'C:\AudioDownload\SHOGB1E.wav'
14:01:30 Import: Direct category & ID import for 'SHOGB1E'
14:01:30 Active set Thu Jun 23 00:00:00 2022
14:01:34 Import: Time adjust 'SHOGB1E' 88543ms now 88542ms
14:01:34 Import: CART timing info added to 'SHOGB1E'
14:01:34 Import Successful: File 'C:\AudioDownload\SHOGB1E.wav' imported as 'SHOGB1E'
14:01:35 Directory Monitor: Searching 'C:\AudioDownload\'
14:01:35 Directory Monitor: 1 files detected
14:01:35 Importing file: 'C:\AudioDownload\SHOGB1F.wav'
14:01:35 Import: Direct category & ID import for 'SHOGB1F'
14:01:35 Active set Thu Jun 23 00:00:00 2022
14:01:36 Import: Time adjust 'SHOGB1F' 22035ms now 22034ms
14:01:36 Import: CART timing info added to 'SHOGB1F'
14:01:36 Import Successful: File 'C:\AudioDownload\SHOGB1F.wav' imported as 'SHOGB1F'
14:01:39 Directory Monitor: Searching 'C:\AudioDownload\'
14:01:39 Directory Monitor: 1 files detected
14:01:39 Importing file: 'C:\AudioDownload\SHOGB2A.wav'
14:01:39 Import: Direct category & ID import for 'SHOGB2A'
14:01:39 Active set Thu Jun 23 00:00:00 2022
14:01:42 Import: Time adjust 'SHOGB2A' 21303ms now 21302ms
14:01:42 Import: CART timing info added to 'SHOGB2A'
14:01:42 Import Successful: File 'C:\AudioDownload\SHOGB2A.wav' imported as 'SHOGB2A'
14:01:47 Directory Monitor: Searching 'C:\AudioDownload\'
14:01:47 Directory Monitor: 1 files detected
14:01:47 Importing file: 'C:\AudioDownload\SHOGB2B.wav'
14:01:47 Import: Direct category & ID import for 'SHOGB2B'
14:01:47 Active set Thu Jun 23 00:00:00 2022
14:01:49 Import: Time adjust 'SHOGB2B' 79217ms now 79216ms
14:01:49 Import: CART timing info added to 'SHOGB2B'
14:01:49 Import Successful: File 'C:\AudioDownload\SHOGB2B.wav' imported as 'SHOGB2B'
14:01:51 Directory Monitor: Searching 'C:\AudioDownload\'
14:01:51 Directory Monitor: 1 files detected
14:01:51 Importing file: 'C:\AudioDownload\SHOGB2C.wav'
14:01:51 Import: Direct category & ID import for 'SHOGB2C'
14:01:51 Active set Thu Jun 23 00:00:00 2022
14:01:55 Import: CART timing info added to 'SHOGB2C'
Oleg
Site Admin
Posts: 2773
Joined: Thu Jan 01, 1970 1:00 am
Contact:

Re: Tail a log file

Post by Oleg »

Look at the example below
It extracts 10 lines near error ("failed")

Code: Select all

;*****************************
;* RoboTask Task file
;* Do not edit in text editor!
;*****************************
 
[Root]
ActionAfterRun=INTEGER|0
Actions=FOLDER
Automat=INTEGER|0
CatID=INTEGER|444033236
Comment=STRINGLIST
ContinueOnError=INTEGER|0
DisableOnError=INTEGER|0
DoNotStopWhenShutdown=INTEGER|0
ExternalName=STRING|"Task1462"
Hide=INTEGER|0
ID=INTEGER|-1290200321
LogOnAsUser=INTEGER|1
Name=STRING|"Text tail (lines near error)"
OnErrorTaskID=INTEGER|-1
Priority=INTEGER|3
RunOnClose=INTEGER|0
RunOnStartup=INTEGER|0
ToLog=INTEGER|3
UnicodeFormat=INTEGER|1
WriteGeneralLog=INTEGER|0

[Actions]
Action1=FOLDER
Action10=FOLDER
Action11=FOLDER
Action12=FOLDER
Action13=FOLDER
Action14=FOLDER
Action15=FOLDER
Action16=FOLDER
Action17=FOLDER
Action18=FOLDER
Action19=FOLDER
Action2=FOLDER
Action20=FOLDER
Action21=FOLDER
Action22=FOLDER
Action23=FOLDER
Action3=FOLDER
Action4=FOLDER
Action5=FOLDER
Action6=FOLDER
Action7=FOLDER
Action8=FOLDER
Action9=FOLDER

[Actions\Action1]
ActionID=STRING|"A_VARIABLES_SET"
Enabled=INTEGER|-1
Name=STRING|"Set variable ""FileName"" with value ""C:\temp\test.txt"""
Params=FOLDER

[Actions\Action1\Params]
expand=STRING|"0"
varname=STRING|"FileName"
varvalue=STRING|"C:\temp\test.txt"

[Actions\Action10]
ActionID=STRING|"A_VARIABLES_INCREMENT"
Enabled=INTEGER|-1
Name=STRING|"Increment variable ""LineNum"""
Params=FOLDER

[Actions\Action10\Params]
incement=STRING|"1"
vaiable=STRING|"LineNum"

[Actions\Action11]
ActionID=STRING|"A_LOOP_END"
Enabled=INTEGER|-1
Name=STRING|"End Loop"

[Actions\Action12]
ActionID=STRING|"A_FLOW_IF"
Enabled=INTEGER|-1
Name=STRING|"If Then //if not found the exit"
Params=FOLDER

[Actions\Action12\Params]
case=STRING|"0"
connection=STRING|"0"
count=STRING|"1"
operator=STRING|"1"
type=STRING|"3"
value1=STRING|"{found}"

[Actions\Action13]
ActionID=STRING|"A_ROBOTASK_LOG"
Enabled=INTEGER|-1
Name=STRING|"Log Message"
Params=FOLDER

[Actions\Action13\Params]
message=STRING|"Error not found"
type=STRING|"3"

[Actions\Action14]
ActionID=STRING|"A_FLOW_EXIT"
Enabled=INTEGER|-1
Name=STRING|"Exit"

[Actions\Action15]
ActionID=STRING|"A_FLOW_ENDIF"
Enabled=INTEGER|-1
Name=STRING|"End If"

[Actions\Action16]
ActionID=STRING|"A_SCRIPT_VBEVALUATE"
Enabled=INTEGER|-1
Name=STRING|"VB Evaluate //get start line"
Params=FOLDER

[Actions\Action16\Params]
expression=STRING|"GetStartLine({LineNum}, 5)"
line00000000=STRING|"function GetStartLine(cnt, delta)"
line00000001=STRING|"  start = cnt - delta + 1"
line00000002=STRING|"  if start < 1 then"
line00000003=STRING|"    start = 1"
line00000004=STRING|"  end if"
line00000005=STRING|"  GetStartLine = start"
line00000006=STRING|"end Function"
line00000008=STRING|"'logmessage(GetStartLine(10, 15))"
linecount=STRING|"9"
loadfromfile=STRING|"1"
variable=STRING|"StartLine"

[Actions\Action17]
ActionID=STRING|"A_ROBOTASK_LOG"
Enabled=INTEGER|-1
Name=STRING|"Log Message"
Params=FOLDER

[Actions\Action17\Params]
message=STRING|"{StartLine}"
type=STRING|"3"

[Actions\Action18]
ActionID=STRING|"A_VARIABLES_SET"
Enabled=INTEGER|-1
Name=STRING|"Set variable ""txt"" with value """""
Params=FOLDER

[Actions\Action18\Params]
expand=STRING|"0"
varname=STRING|"txt"

[Actions\Action19]
ActionID=STRING|"A_LOOP_SIMPLE"
Enabled=INTEGER|-1
Name=STRING|"Simple Loop"
Params=FOLDER

[Actions\Action19\Params]
begin=STRING|"{StartLine}"
end=STRING|"{Add({StartLine},9)}"
variable=STRING|"NUM"

[Actions\Action2]
ActionID=STRING|"A_VARIABLES_SET"
Enabled=INTEGER|-1
Name=STRING|"Set variable ""LineNum"" with value ""1"""
Params=FOLDER

[Actions\Action2\Params]
expand=STRING|"0"
varname=STRING|"LineNum"
varvalue=STRING|"1"

[Actions\Action20]
ActionID=STRING|"A_TXT_EXTRACTLINE"
Enabled=INTEGER|-1
Name=STRING|"TXT Extract Line"
Params=FOLDER

[Actions\Action20\Params]
external=STRING|"1"
filename=STRING|"{FileName}"
linecount=STRING|"0"
linenum=STRING|"{num}"
variable=STRING|"Line"

[Actions\Action21]
ActionID=STRING|"A_VARIABLES_SET"
Enabled=INTEGER|-1
Name=STRING|"Set variable ""txt"" with value ""{txt}{Line}{Eol}"""
Params=FOLDER

[Actions\Action21\Params]
expand=STRING|"1"
varname=STRING|"txt"
varvalue=STRING|"{txt}{Line}{Eol}"

[Actions\Action22]
ActionID=STRING|"A_LOOP_END"
Enabled=INTEGER|-1
Name=STRING|"End Loop"

[Actions\Action23]
ActionID=STRING|"A_DIALOG_SHOWTEXT"
Enabled=INTEGER|-1
Name=STRING|"Show Text"
Params=FOLDER

[Actions\Action23\Params]
autoclose=STRING|"0"
fromfile=STRING|"1"
line00000000=STRING|"{Txt}"
linecount=STRING|"1"
timeout=STRING|"10"
title=STRING|"Text tail"

[Actions\Action3]
ActionID=STRING|"A_VARIABLES_SET"
Enabled=INTEGER|-1
Name=STRING|"Set variable ""found"" with value ""false"""
Params=FOLDER

[Actions\Action3\Params]
expand=STRING|"0"
varname=STRING|"found"
varvalue=STRING|"false"

[Actions\Action4]
ActionID=STRING|"A_MISC_COMMENT"
Enabled=INTEGER|-1
Name=STRING|"//Search error "
Params=FOLDER

[Actions\Action4\Params]
comment=STRING|"Search error "

[Actions\Action5]
ActionID=STRING|"A_LOOP_TEXT"
Enabled=INTEGER|-1
Name=STRING|"Text Loop"
Params=FOLDER

[Actions\Action5\Params]
destvar=STRING|"LINE"
file=STRING|"{fileName}"
linecount=STRING|"0"
sourcetext=STRING|"0"

[Actions\Action6]
ActionID=STRING|"A_FLOW_IF"
Enabled=INTEGER|-1
Name=STRING|"If Then"
Params=FOLDER

[Actions\Action6\Params]
case=STRING|"0"
connection=STRING|"0"
count=STRING|"1"
operator=STRING|"6"
type=STRING|"0"
value1=STRING|"failed"
value2=STRING|"{line}"

[Actions\Action7]
ActionID=STRING|"A_VARIABLES_SET"
Enabled=INTEGER|-1
Name=STRING|"Set variable ""found"" with value ""True"""
Params=FOLDER

[Actions\Action7\Params]
expand=STRING|"0"
varname=STRING|"found"
varvalue=STRING|"True"

[Actions\Action8]
ActionID=STRING|"A_LOOP_BREAK"
Enabled=INTEGER|-1
Name=STRING|"Break"

[Actions\Action9]
ActionID=STRING|"A_FLOW_ENDIF"
Enabled=INTEGER|-1
Name=STRING|"End If"

Oleg Yershov
Post Reply