Sunday, September 27, 2009

Extract info from Logs

One of my projects used to update set of log files for each action and scheduled actions. Exceptions are logged in those log files. Log file size is maintained upto 5 MB. We are unable to get the proper logs, if scripts are running more than 15 minutes. Entire suite runs almost 75 hours continuously.

Many times, we were unable to re-look at the exceptions in the logs for particular test case failed time. I thought to develop a vbscript to capture the exceptions from log file for frequent intervals and then write into another text file. It can be used for manual testing too. Here I made two things. First is, script has to parse the log file for the given string. Second is, script should not update the log information, which is already available. I meant, the same information should not be added multiple times.

Code to take only unique info It returns the information as Array. Array size is determined in run-time.


'-------------------------------------------------------------------------
' Method : TrimArrayToExtract
' Author : T. Palani Selvam
' Purpose : Remove the array elements based on given info (sItemToCheck).
' Parameters: arrInput - Array String, Contains logging message.
' sItemToCheck - String, to check the element match. Last element in the text file
' Returns : String - Array. Returns remaining elements from the given array.
' Caller : - Nil
' Calls : - Nil
'-------------------------------------------------------------------------
Function TrimArrayToExtract (arrInput, sItemToCheck)
Dim iArrItem
Dim sTemp
Dim arrOutput()

iIndex=0
If (IsNull(sItemToCheck) Or IsEmpty(sItemToCheck) Or (Trim(sItemToCheck)= "")) Then
TrimArrayToExtract=arrInput
Exit Function
Else
If (IsArray(arrInput)) Then
'If (Not (IsNull(arrInput) Or IsEmpty(arrInput)) And (UBound(arrInput)> 1)) Then
If (Not (IsNull(arrInput) Or IsEmpty(arrInput) Or (UBound(arrInput)=0)) ) Then
'WScript.Echo "ArrInput : " & arrInput
For iArrItem=0 to UBound(arrInput) ''-1
ReDim Preserve arrOutput(iIndex)
sTemp=arrInput(iArrItem)
If (sTemp=sItemToCheck) Then
iIndex=0
Erase arrOutput
Else
arrOutput(iIndex)=sTemp
iIndex=iIndex+1
End If

Next
End If
End If
End If

TrimArrayToExtract=arrOutput

End Function


Code for Extracting the information from Log file It returns the information in Array.

'--------------------------------------
' Method : ExtractInfoFromLog
' Author : T. Palani Selvam
' Purpose : To extract the lines from a log file based on given info.
' Parameters: sFileName - String, contains the log filename with full path.
' sInfo2Extract - String, Info to search.
' Returns : Array of String
' Caller : - Nil
' Calls : - Nil
'--------------------------------------
Function ExtractInfoFromLog (sFileName, sInfo2Extract)

Dim objFSO, objTextFile, sLine
Dim iPos
Dim arrExtract()
Dim iLinesToDo, iArrIndex, iLimit

Const ForReading=1
Const NoOfLines=10


iArrIndex=0
iLinesToDo=0
iLimit=1

Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(sFileName) Then

Set objTextFile = objFSO.OpenTextFile(sFileName, ForReading)

Do while Not objTextFile.AtEndOfStream
sLine = objTextFile.ReadLine
'Wscript.Echo sLine
If (iLinesToDo > 0) Then
arrExtract(iArrIndex)=sLine
iLinesToDo=iLinesToDo-1
iArrIndex=iArrIndex+1
Else

iLinesToDo=0
iPos=InStr(sLine,sInfo2Extract)
'WScript.Echo("iPos " & CStr(iPos))
If (iPos>0) Then
'WScript.Echo("Entered into If loop to extract from line: " & sLine)

'' iLimit = UBound (arrExtract) + NoOfLines
iLimit = iLimit + NoOfLines
ReDim Preserve arrExtract(iLimit)

arrExtract(iArrIndex)=sLine
iLinesToDo=NoOfLines-1
iArrIndex=iArrIndex+1

Else
iLinesToDo=0

End If
End If

Loop

End If

objTextFile.Close

Set objTextFile = Nothing
Set objFSO = Nothing

ExtractInfoFromLog=arrExtract
End Function

Note: Few user-defined functions might be used.

No comments: