Skip to content

mintmousse.addGlobalLogSink

Used to add a custom sink to direct log messages from across all threads in a single place. This function can only be used on the main thread.

I recommend checking out the logging sinks that the library implements to fully understand how you can implement your own.

Thread behaviour

This function can only be called on the main thread. If you can architect it, it's recommended to use the pre-thread sinks added via mintmousse.addLogSink. So, then your log isn't having to wait to be processed on the main thread.

Synopsis

mintmousse.addGlobalLogSink( sink, flushFunc )

Parameters

sink function
A function, with the signature (level, logger, time, debugInfo, message...). See below for details
flushFunc function
A function, with the signature (forced). See mintmousse.flushLogs for details

Sink Function Signature

level level
A string indicating the level of the log, "info", "warning", "error", "fatal", "debug".
logger logger

The logger that created the log.

You shouldn't try to use any other function of the logger when it is in a sink other than (Logger):getAncestry and (Logger).inspect.

time number
UNIX time in seconds, with microseconds. By default, this value is obtained by socket.gettime to get an accurate system time.
debugInfo string or nil
Depending on the level, and the config -- TODO link, a simple traceback string is passed along the lines of funcName@fileName#lineNumber, this format isn't guaranteed, for example code in the file scope will be fileName#lineNumber as they aren't contained within a named function.
message... ANY
The varargs of the message passed from the log function.

Returns

Nothing.

Examples

Not necessarily working code, but an idea.

local http = require("socket.http")

local logs = { }
mintmousse.addGlobalLogSink(
  function(level, logger, time, debugInfo, ...) -- Sink function
    if (level == "error" or info == "fatal") then
      table.insert(logs, table.concat({ ... }, " "))
    end
  end,
  function(forced) -- Flush function
    http.request({
        url = "http://example.com/endpoint/?logs=" .. table.concat(logs, "&"),
        method = "POST",
      })
  end
)

See Also