Skip to content
Weaverv0.5.0

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}")
}
  • The levels, from the least severe: trace, debug, info, warn, error, fatal.
  • error and fatal take the error itself, which carries where it arose: the event shows that place, not the line that logs it.
  • Fields are typed, .IntV, .FloatV, .BoolV and .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: fatal logs, then returns. Stopping is the business of panic, Process.exit, or an error out of main.

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=trace

off silences a logger, or everything; fatal is always written. The program’s own module logs as main, WEAVER_LOG=main=debug.

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())
  • MemoryHandler keeps the events, for a test to read with events().
  • NullHandler writes 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, and handle.

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.