Log Analysis Script (Python)
In this article:
Scripting in Python 3.4 is supported by ULogViewer 3.0+. Suggest reading Scripting in Application (Python) first to get the basis of scripting.
Sub scripts
There are 2 sub-scripts for each log analysis script. The context when executing script will be shared across all sub-scripts belong to same script.Script to Setup Analysis
To perform custom actions before analyzing logs. The script will be invoked one time after applying the log analysis script. Example:
import re
context.Data["messageRegex"] = re.compile("ULogViewer")
context.Data["messageRegex"] = re.compile("ULogViewer")
Script to Analyze Log
Script to be invoked for each log to be analyzed. The returned value should be boolean to indicate whether results added by script should be taken or not. ⬆️ Back to topContext
The followings are members of context other than basic members:-
AddResult(Result result)
Add analysis result. The followings are members of Result structure:
-
BeginningLog: ILog
The beginning log which is related to result.
-
ByteSize: int
The size in bytes related to result.
-
Duration: System.TimeSpan
The duration which is related to result.
-
EndingLog: ILog
The ending log which is related to result.
-
Log: ILog
The log which is related to result.
-
Message: string
Message of result.
-
Quantity: int
The quantity which is related to result.
-
Type: ResultType
Type of result. Please refer to Type of Analysis Result for possible values.
-
BeginningLog: ILog
-
Log: ILog
The log to be analyzed. The followings are members of ILog:
-
GetProperty(name: string, defaultValue: object): object
Get property of log with specific name. The defaultValue will be returned if property doesn't exist. Please refer to Name of Log Properties for valid names.
-
Text: string
Get full text of log. The format of text is same as text for log text filtering.
-
GetProperty(name: string, defaultValue: object): object
import re
regex = context.Data["messageRegex"]
message = context.Log.GetProperty("Message", None)
if message and regex.search(message):
result = Result()
result.Log = context.Log
result.Message = "Log found!!"
result.Type = ResultType.Information
context.AddResult(result)
return True
return False
⬆️ Back to top
regex = context.Data["messageRegex"]
message = context.Log.GetProperty("Message", None)
if message and regex.search(message):
result = Result()
result.Log = context.Log
result.Message = "Log found!!"
result.Type = ResultType.Information
context.AddResult(result)
return True
return False
Type of Analysis Result
All types of analysis results are defined in ResultType enumeration:- Error (Default)
- Warning
- OperationStart
- OperationEnd
- Increase
- Decrease
- Steady
- Fast
- Slow
- Checkpoint
- TimeSpan
- Performance
- Frequency
- Trend
- Information
- SkippedOperation
- Debug
Name of Log Properties
-
BeginningTimeSpan
Beginning time span. The type is System.TimeSpan.
-
BeginningTimestamp
Beginning timestamp. The type is System.DateTime.
-
Category
Category. The type is string.
-
DeviceId
ID of device. The type is string.
-
DeviceName
Name of device. The type is string.
-
EndingTimeSpan
Ending time span. The type is System.TimeSpan.
-
EndingTimestamp
Ending time span. The type is System.DateTime.
-
Event
Event. The type is string.
- Extra1
- Extra10
- Extra11v4.0+
- Extra12v4.0+
- Extra13v4.0+
- Extra14v4.0+
- Extra15v4.0+
- Extra16v4.0+
- Extra17v4.0+
- Extra18v4.0+
- Extra19v4.0+
- Extra2
- Extra20v4.0+
- Extra3
- Extra4
- Extra5
- Extra6
- Extra7
- Extra8
- Extra9
Extra information. The type is string.
-
FileName
Name of file to read log. The type is string.
-
Level
Level of log. The type is LogLevel defined by ULogViewer. The followings are values defined in LogLevel from the highest priority to the lowest:
- Fatal
- Error
- Failure
- Warn
- Success
- Info
- Debug
- Trace
- Verbose
- Undefined
-
LineNumber
Line number. The type is int.
-
Message
Message. The type is string.
-
ProcessId
ID of process. The type is int.
-
ProcessName
Name of process. The type is string.
-
ReadTime
Timestamp of log to be read. The type is System.DateTime.
-
SourceName
Name of log source. The type is string.
-
Summary
Summary. The type is string.
-
Tags
Tags. The type is string.
-
ThreadId
ID of thread. The type is int.
-
ThreadName
Name of thread. The type is string.
-
TimeSpan
Time span. The type is System.TimeSpan.
-
Timestamp
Timestamp. The type is System.DateTime.
-
Title
Title. The type is string.
-
UserId
ID of user. The type is string.
-
UserName
Name of user. The type is string.