delog
is a fast, extensible, optionally zero-impact logging module for
Lua.
-- Import and configure the module
local log = require("delog").level("trace")
local levels = { "trace", "debug", "info", "warn", "error", "fatal" }
for _, level in ipairs(levels) do
log[level]("This is a message of '${}' level", level)
end
-
Multiple logging levels:
trace
,debug
,info
,warn
,error
, andfatal
. -
Lazy evaluation of log format arguments.
-
Convenient log format string formatting, supporting string.format modifiers (
"${%i}"
), built-in pretty-printers for tables ("${%p}"
and"${%pp}"
), and table field extraction ("${fieldname}"
). -
Support for plugging-in additional user-provided formatters.
-
delog
filter for stripping logging calls from Lua sources. The filter takes Lua source code as input, removed the calls to functions in thedelog
module. Stripped sources can then be used to run applications for which logging introduces performance issues. -
Colored console output, with optional automatic detection of terminals (requires lua-isatty, lua-term, luaposix, or the LuaJIT FFI).
Sets the logging level to l
, which should be one of:
"trace"
"debug"
"info"
"warn"
"error"
"fatal"
By default the logging level is "warn"
. Enabling a certain level also
enables the rest of levels over it, e.g. enabling "error"
also enables
"fatal"
, and enabling "trace"
enables all logging levels.
Sets the string which gets prepended to each log line. The default value produces lines like the following, optionally coloered:
[WARN 20:45:14] This is a log message.
This is achieved with the following “prepend” format string (which is
available as delog.PREPEND_DEFAULT
):
"${%color%}[${%level-upper%} ${%time-hhmmss%}] ${%nocolor%}"
Additionally, the module provides two more “prepend” format strings ready to use, aimed for debugging:
delog.PREPEND_DEBUG
, same as the default, plus the source file name and line where the logging function was called.delog.PREPEND_DEBUG_FUNC
, same asPREPEND_DEBUG
, plus the name of the function where the logging function was called (if available).
Tip: If you are planning to use delog
for debugging, probably you want
to load the module as follows:
local log = require("delog").level("debug")
log.prepend(log.PREPEND_DEBUG) -- or PREPEND_DEBUG_FUNC
Sets the string which gets appended to each log line. The default value is
a newline character ("\n"
).
Manually sets whether to use ANSI color escape sequences. This is useful if the module cannot detect by itself whether the output is being sent to a terminal, then colors can still be obtained by using this function:
local log = require("delog").color(true)
log.warn("This will be always colored")
Sets the logging output. If a string is passed, it is interpreted as a
file path, and the file will be opened in append mode. By default,
output is sent to io.stderr
.
Registers a custom formatting function (func
), with a given format name
.
This allows client code to supply additional format specifiers. Example:
local log = require("delog").format("upper", function (value)
return tostring(value):upper()
end)
log.warn("${%upper}", "this will be output in upper case")
Formats a message and writes and logs it in the "trace"
level.
Formats a message and writes and logs it in the "debug"
level.
Formats a message and writes and logs it in the "info"
level.
Formats a message and writes and logs it in the "warn"
level.
Formats a message and writes and logs it in the "error"
level.
Formats a message and writes and logs it in the "fatal"
level.