Log

The Log module provides a set of logging APIs for debugging purposes. The logging module has the following features:

  • A total of 3 log levels are supported for general purpose log messages: INFO, WARN, and ERR.

  • The file name and line number are captured with each log message.

  • Logging to chat can be enabled via the Log.SetLoggingToChatEnabled() API and includes colored severity levels.

  • A buffer of the most recent 200 log entries is kept for potential retrieval latter (i.e. from an error handler).

Example

The following demonstrates a simple usage of this module.

-- Demo.lua
local MyModule = select(2, ...).MyModule
local ExecutionTime = MyModule:From("LibTSMUtil"):Include("Util.Log")

Log.Info("My favorite number is %d", random(0, 100))
-- 12:15:24.615 [INFO] {Demo.lua:4} My favorite number is 42

API

class Util.Log: LibTSMModule
staticmethod SetCurrentThreadNameFunction(func: fun(): string)

Registers a function for getting the name of the current thread to include in log messages.

Parameters:

func (fun(): string) – A function which returns the name of the currently-running thread

staticmethod SetLoggingToChatEnabled(enabled: boolean, ignoreBuffered?: boolean)

Enables or disables printing log messages to chat.

Parameters:
  • enabled (boolean) – The logging state to set

  • ignoreBuffered? (boolean) – Ignore any buffered logs

staticmethod Length(): number

Gets the length of the log buffer.

staticmethod Get(index: number): (
    severity: LogSeverity,
    location: string,
    timeStr: string,
    msg: string
)

Gets a log entry from the log buffer.

Parameters:

index (number) – The index within the buffer

staticmethod RaiseStackLevel(amount?: number)

Raises the stack level of log messages.

Parameters:

amount? (number) – The amount to raise the stack level by (defaults to 1)

staticmethod LowerStackLevel(amount?: number)

Lowers the stack level of log messages.

Parameters:

amount? (number) – The amount to lower the stack level by (defaults to 1)

staticmethod StackTrace(maxLines?: number)

Logs a stack trace.

Parameters:

maxLines? (number) – The maximum number of lines (defaults to all available)

staticmethod Info(...: string)

Logs a formatted message at the info level.

staticmethod Warn(...: string)

Logs a formatted message at the warning level.

staticmethod Err(...: string)

Logs a formatted message at the error level.

staticmethod Custom(severity: LogSeverity, location: string, ...: string)

Logs a formatted message with custom options.

Parameters:

location (string) – The location for the log message

staticmethod PrepareFormatArgs(...: any): (...: unknown)

Prepares format args for logging.

Parameters:

... (any) – The format arguments