Log Data Source 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.

Samples

The following is sample script to use user-provided pattern (regular expression) to read and filter raw log from file:
⬆️ Back to top

Sub 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 opening reader for reading raw log lines. The type of returned value is LogDataSourceState. Returning LogDataSourceState.ReaderOpened if reader opened successfully, Or returning 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
    Unclassified error was occurred.
Example:
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

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 raw log line (string), or Null if there is no more raw log line can be 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

Context

The followings are members of context other than basic members:
  • Options: LogDataSourceOptions
    Data source options for opening reader and reading raw log lines. The followings 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
⬆️ Back to top