Logging
Module weaver.log: its API, wv doc weaver.log.
Code logs through named loggers; one handler receives the events of all of them, decides which to keep and writes them. A library never chooses where its events go: the application does, in one place.
import weaver.log.{Logger}
val log = Logger.forModule() // named after the module, `app.server`
fn serve() { log.info("listening", ("port", .IntV(8080))) log.warn("slow request", ("path", .Str(path)), ("ms", .IntV(ms))) save(order) catch e => log.error(e, "saving order ${order.id}")}Levels and fields
Section titled “Levels and fields”- The levels, from the least severe:
trace,debug,info,warn,error,fatal. errorandfataltake the error itself, which carries where it arose: the event shows that place, not the line that logs it.- Fields are typed,
.IntV,.FloatV,.BoolVand.Str, so that a structured handler writes them as they are. - A disabled event costs almost nothing: its message and its fields are not even computed.
- A level is a severity and nothing more:
fatallogs, then returns. Stopping is the business ofpanic,Process.exit, or an error out ofmain.
Choosing what is written: WEAVER_LOG
Section titled “Choosing what is written: WEAVER_LOG”The default handler writes one line per event to standard error, at the
levels WEAVER_LOG gives: a bare level for every logger, name=level for a
logger and those below it, the most specific name winning; info without
it.
WEAVER_LOG=warn,weaver.net=debug,app.db=traceoff silences a logger, or everything; fatal is always written. The
program’s own module logs as main, WEAVER_LOG=main=debug.
Another handler
Section titled “Another handler”handler is a task val: bound around a call, it holds for everything the
call does, the tasks it launches and the libraries it calls included.
handler.bind(StderrHandler(level: .debug), () => run())MemoryHandlerkeeps the events, for a test to read withevents().NullHandlerwrites nothing.withFields([("request", .Str(id))], () => handle(req))adds fields to every event of the call.- Any other destination, JSON, files, a collector, plugs in by
implementing
LogHandler:enabled, asked before an event is built, andhandle.
A panic logs itself at the fatal level through the handler of its task,
so that a program sending its events elsewhere receives its panics there
too.