Skip to content

Log

The Log module provides a set of logging APIs for debugging purposes:

  • A total of 3 log levels are supported: INFO, WARN, and ERR.
  • The file name and line number are captured with each log message.
  • Logging to chat can be enabled via Log.SetLoggingToChatEnabled() and includes colored severity levels.
  • A buffer of the most recent 200 log entries is kept for potential retrieval later (i.e. from an error handler).

Example

lua
-- Demo.lua
local MyModule = select(2, ...).MyModule
local Log = 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

Inherits: LibTSMModule

Functions

Custom

lua
function Util.Log.Custom(severity, location, message, ...)

Logs a formatted message with custom options.

Parameters:

NameTypeDescription
severityLogSeverity
locationstringThe location for the log message
messagestringThe message or format string
...string | number | booleanThe format arguments

Err

lua
function Util.Log.Err(message, ...)

Logs a formatted message at the error level.

Parameters:

NameTypeDescription
messagestringThe message or format string
...string | number | booleanThe format arguments

Get

lua
function Util.Log.Get(index)
  -> "TRACE" | "INFO" | "WARN" | "ERR", string, string, string

Gets a log entry from the log buffer.

Parameters:

NameTypeDescription
indexnumberThe index within the buffer

Returns:

NameTypeDescription
severity"TRACE" | "INFO" | "WARN" | "ERR"
locationstring
timeStrstring
msgstring

Info

lua
function Util.Log.Info(message, ...)

Logs a formatted message at the info level.

Parameters:

NameTypeDescription
messagestringThe message or format string
...string | number | booleanThe format arguments

Length

lua
function Util.Log.Length()
  -> number

Gets the length of the log buffer.

Returns:

TypeDescription
number

LowerStackLevel

lua
function Util.Log.LowerStackLevel(amount)

Lowers the stack level of log messages.

Parameters:

NameTypeDescription
amountnumberThe amount to lower the stack level by (defaults to 1)

PrepareFormatArgs

lua
function Util.Log.PrepareFormatArgs(...)
  -> ...any

Prepares format args for logging.

Parameters:

NameTypeDescription
...string | number | booleanThe format arguments

Returns:

TypeDescription
...any

RaiseStackLevel

lua
function Util.Log.RaiseStackLevel(amount)

Raises the stack level of log messages.

Parameters:

NameTypeDescription
amountnumberThe amount to raise the stack level by (defaults to 1)

SetCurrentThreadNameFunction

lua
function Util.Log.SetCurrentThreadNameFunction(func)

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

Parameters:

NameTypeDescription
funcfun(): string?A function which returns the name of the currently-running thread

SetLoggingToChatEnabled

lua
function Util.Log.SetLoggingToChatEnabled(enabled, ignoreBuffered)

Enables or disables printing log messages to chat.

Parameters:

NameTypeDescription
enabledbooleanThe logging state to set
ignoreBufferedbooleanIgnore any buffered logs

StackTrace

lua
function Util.Log.StackTrace(maxLines)

Logs a stack trace.

Parameters:

NameTypeDescription
maxLinesnumberThe maximum number of lines (defaults to all available)

Warn

lua
function Util.Log.Warn(message, ...)

Logs a formatted message at the warning level.

Parameters:

NameTypeDescription
messagestringThe message or format string
...string | number | booleanThe format arguments