March Docs

Logger

Logger module: structured logging with levels and persistent context.

Log levels (ascending severity): Debug < Info < Warn < Error. The global level filter is set with set_level/1; messages below the current level are silently discarded.

with_context/2 attaches a key-value pair to every subsequent log entry. clear_context/0 removes all attached context.

Output goes to stderr in the format: [LEVEL] message {key=value, key2=value2}

Usage: Logger.set_level(Logger.Info) Logger.with_context("request_id", "abc123") Logger.info("handling request") Logger.warn("slow query") Logger.error("db connection lost") Logger.log_with(Logger.Info, "user signed in", [("user", "alice")])

Types

typeLevelLevel = Debug | Info | Warn | Error#

Functions

fnlevel_to_intlevel_to_int(l)#
fnlevel_to_stringlevel_to_string(l)#
fnint_to_levelint_to_level(n)#
fnlevel_from_stringlevel_from_string(s)#

Parse a log level from a string (case-insensitive). Returns None for unknown strings.

fnlevel_ranklevel_rank(l)#

Return the numeric rank of a log level: Debug=0, Info=1, Warn=2, Error=3.

fnlevel_enabledlevel_enabled(level, min_level)#

Return true if level is at least as severe as min_level.

fnformat_entryformat_entry(level, msg)#

Format a log entry as a simple string: [LEVEL] message

fnlog_iflog_if(level, min_level, msg)#

Log at level only if level is enabled relative to min_level.

fnset_levelset_level(level)#

Set the minimum log level. Messages below this level are discarded.

fnget_levelget_level()#

Get the current minimum log level.

fnwith_contextwith_context(key, value)#

Add a key-value pair to the persistent log context.

fnclear_contextclear_context()#

Remove all persistent context key-value pairs.

fndebugdebug(msg) do do_log(Debug, msg, Nil) end#

Log a Debug-level message.

fninfoinfo(msg) do do_log(Info, msg, Nil) end#

Log an Info-level message.

fnwarnwarn(msg) do do_log(Warn, msg, Nil) end#

Log a Warn-level message.

fnerrorerror(msg) do do_log(Error, msg, Nil) end#

Log an Error-level message.

fnloglog(level, msg) do do_log(level, msg, Nil) end#

Log a message at the given level.

fnlog_withlog_with(level, msg, metadata)#

Log a message at the given level with extra metadata. metadata is a List((String, String)) of key-value pairs.

Logger.log_with(Logger.Info, "request done", [("status", "200"), ("ms", "42")])