Log Data Source Script (Python)
In this article:
Scripting in Python 3.4 is supported by ULogViewer 3.0+. We recommend reading Scripting in Application (Python) first to learn the basics of scripting.
Samples
The following is sample script to use user-provided pattern (regular expression) to read and filter raw log from file: ⬆️ Back to topSub Scripts
There are 3 sub scripts in each log data source script:Script to Open Raw Log Reader (Required)
The script will be invoked one time when trying to open a reader for reading raw log lines. The type of returned value is LogDataSourceState. Return LogDataSourceState.ReaderOpened if the reader is opened successfully, or return the following values for failure cases:-
LogDataSourceState.SourceNotFound
The source of raw log cannot be found.
-
LogDataSourceState.ExternalDependencyNotFound
One or more external dependencies were not found.
-
LogDataSourceState.UnclassifiedError
An unclassified error occurred.
fileName = Context.Options.FileName
if not fileName:
log_error("No file name specified")
return LogDataSourceState.UnclassifiedError
context.Data["reader"] = StreamReader(fileName, Encoding.UTF8)
return LogDataSourceState.ReaderOpened
if not fileName:
log_error("No file name specified")
return LogDataSourceState.UnclassifiedError
context.Data["reader"] = StreamReader(fileName, Encoding.UTF8)
return LogDataSourceState.ReaderOpened
Script to Read Raw Log Line (Required)
The script will be invoked every time when trying to read raw log line. The returned value should be a raw log line (string), or Null if there are no more raw log lines to read. Example:
return context.Data["reader"].ReadLine()
Script to Close Raw Log Reader (Optional)
The script will be invoked one time when closing reader for reading raw log lines. There is no returned value needed. Example:
reader = context.Data["reader"]
if reader:
reader.Dispose()
⬆️ Back to top
if reader:
reader.Dispose()
Context
The following are members of context other than basic members:-
Options: LogDataSourceOptions
Data source options for opening reader and reading raw log lines. The following are members of LogDataSourceOptions:
- Category: string
- Command: string
- ConnectionString: string
- Encoding: System.Text.Encoding
- EnvironmentVariables: IDictionary<String, String>v4.0+
- FileName: string
- FormatJsonData: bool
- FormatXmlData: bool
- IncludeStandardError: bool
- IPEndPoint: System.Net.IPEndPoint
-
IsOptionSet(optionName: string): bool
Check whether specific option was set with value or not.
- IsResourceOnAzure: bool
- Password: string
- ProcessId: System.Nullable<int>v4.0+
- ProcessName: stringv4.0+
- QueryString: string
- SetupCommands: IList<string>
- TeardownCommands: IList<string>
- UserName: string
- Uri: System.Uri
- WorkingDirectory: string