Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Logging

When something goes wrong in production, you want to know which request failed, which statements it ran, and how long they took. rind writes all of this as events of tracing, the standard logging library of async Rust. You do not need to enable anything: the events are always written. When nothing reads them, they cost almost nothing.

Coming from Django: this replaces the LOGGING setting and the django.request and django.db.backends loggers. In rind, the target of an event plays the part of a logger’s name (rind::sql instead of django.db.backends), and the RUST_LOG variable plays the part of the LOGGING dictionary.

Seeing the logs

An application that uses rind’s command line (rind::cli::main, as the railway does) already has a writer: the command line sets one up before running any command. Two environment variables control it:

  • RUST_LOG says what is written. It is info by default. Set it to info,rind::sql=debug to also see every statement.
  • RIND_LOG=json writes one JSON object per line, which is what a log collector reads. Without it, the events are written as text.

The events go to the standard error. For example, to serve the railway with every statement, as JSON:

$ RIND_LOG=json RUST_LOG=info,rind::sql=debug cargo run -p sample -- serve

A list of the stations then writes:

{"level":"DEBUG","fields":{"message":"statement","kind":"select","table":"network_station","ms":1.45,"rows":2},
 "target":"rind::sql","span":{"method":"GET","operation":"list","resource":"stations","name":"request"}, …}
{"level":"INFO","fields":{"message":"answered","status":200,"ms":3.09},"target":"rind::request", …}

Each statement is written inside the span of its request: a span is a piece of context that tracing attaches to every event written while it is open. So each statement says which request ran it (here, the list of stations by GET).

What rind writes

Each call of a resource is a span, named request, under the target rind::request. Its fields are the method, the resource, the operation, the object’s id, and the user once authenticated. When the call is answered, an answered event gives the status and the time in milliseconds.

Each statement is an event under the target rind::sql:

  • at debug, every statement: its kind, its table, its time and its rows; and a committed event with the tables a write changed;
  • at warn, a statement slower than RIND_SLOW_QUERY_MS milliseconds (500 by default), with its SQL;
  • at trace, the SQL of every statement.

A request that fails on the server’s side (a database error, a bug) is an error event under the target rind_api::error. The client gets a 500 with {"detail": "Internal server error."}, without the cause; the cause is in the log.

The plugins write what they could not do, at warn or error, under a target of their own: rind::cache (Redis unreachable), rind::usage (the usage log is full, or a batch could not be written), rind::storage (a file the database names is missing from the storage), rind::health (a readiness check failed), rind::mail, rind::job, rind::images, and rind::auth.

The parameters of a statement are never logged. They are the users’ data: names, addresses, passwords. Only the SQL text, with its placeholders ($1), is written, and only at trace.

Using your own writer

An application that wants another writer (OpenTelemetry, a file, a different format) sets it up in main before calling rind::cli::main. The command line sees that a writer exists already, and leaves it in place:

fn main() -> std::process::ExitCode {
    // Set up your own `tracing` subscriber here.
    rind::cli::main(sample::app())
}

A program that logs with the older log crate can receive rind’s events too: turn on the log feature of tracing in your Cargo.toml, and tracing also writes each event as a log record when no tracing writer is set up.

Reference

TargetLevelWhat is written
rind::requestinfoa span per resource call (method, resource, operation, id, user), and answered at its end, with the status and the time
rind::sqldebugeach statement: its kind, its table, its time, its rows; a failed statement (statement failed, with the error); committed with the tables a write changed
rind::sqlwarna statement slower than RIND_SLOW_QUERY_MS (500 by default), with its SQL
rind::sqltracethe SQL of each statement
rind_api::errorerrora request that failed on the server’s side (the client gets a 500 without the cause)
rind::usage, rind::cache, rind::storage, rind::health, …warn, errorwhat a plugin could not do: a full usage log, an unreachable Redis, a file the database names and the storage does not have, a failed readiness check
VariableDefaultEffect
RUST_LOGinfowhat is written, as tracing-subscriber’s EnvFilter reads it
RIND_LOGtextjson: one JSON object per line
RIND_SLOW_QUERY_MS500the time past which a statement is logged at warn

The writer is set up by rind::cli::logging(), which rind::cli::main calls before any command. You can call it yourself in a program that does not use the command line.