Guide

Logging and error collection

Logging and error collection

Choose a console style

The default console prints a short line per event. A failure adds its reason to the same line. For timestamps, fields, and stack frames, choose the detailed style.

Pooka
import loggit.{Colour, ConsoleStyle}

main() {
  let assert Ok(_) =
    loggit.install_console_with_style(Colour.Auto, ConsoleStyle.Detailed)
  loggit.info("catalogue ready")
}

Automatic colour follows the logging device or FORCE_COLOR=1, with NO_COLOR taking precedence. Both styles escape control characters and keep the handler's destination. Collectors receive the same structured events.

Attach request context

loggit.with_fields applies a list of string key/value pairs to logging on the current Erlang process. Nested scopes temporarily override earlier values. Fields given directly to loggit.log or loggit.report take priority. The earlier context is restored if the callback raises. Pass context as data when starting work on a different process.

Prefer a normalized route, request ID and component name to dumping an entire request. Loggit forwards explicit fields as supplied. Keep credentials, cookies, request bodies and other sensitive values out of those fields. Inspect exception reasons before persisting them as they can contain input.

Connect a tracker

At application startup, set an OTP node threshold and start one collector:

Pooka
let assert Ok(_) = loggit.set_level(Level.Info)
let assert Ok(_) = loggit.subscribe(Level.Error, 256, fn(event) {
  enqueue_incident(event)
})

Here enqueue_incident is the application's own function returning Nil. The callback runs on a dedicated process. It receives typed Event values, including timestamp, level, optional origin, fields, and optional Failure. A failure has a kind, printable reason, optional source address, and typed stack frames. attempt captures synchronous exceptions, and report emits the captured failure through OTP. Native proc_lib crash reports also supply structured failures. Other native reports retain a printable message. A web framework can use the failure kind and first application frame for grouping, and keep request IDs as occurrence metadata.

The collector capacity includes the in-flight callback. When full, it drops new events and counts them. Exceptions in the callback are counted separately via sink_stats. Logs produced by the collector do not enter its own queue. OTP's default handler can still print them. Keep the callback short and hand off persistence separately.

flush(timeout_ms) waits for earlier events sent by its calling process. It does not wait for independent producers to finish. Stop producers first if you need a clean shutdown, flush, then call unsubscribe. Unsubscribing removes the handler and stops its worker.