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
LOGGINGsetting and thedjango.requestanddjango.db.backendsloggers. In rind, the target of an event plays the part of a logger’s name (rind::sqlinstead ofdjango.db.backends), and theRUST_LOGvariable plays the part of theLOGGINGdictionary.
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_LOGsays what is written. It isinfoby default. Set it toinfo,rind::sql=debugto also see every statement.RIND_LOG=jsonwrites 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 acommittedevent with the tables a write changed; - at
warn, a statement slower thanRIND_SLOW_QUERY_MSmilliseconds (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
| Target | Level | What is written |
|---|---|---|
rind::request | info | a span per resource call (method, resource, operation, id, user), and answered at its end, with the status and the time |
rind::sql | debug | each 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::sql | warn | a statement slower than RIND_SLOW_QUERY_MS (500 by default), with its SQL |
rind::sql | trace | the SQL of each statement |
rind_api::error | error | a 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, error | what 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 |
| Variable | Default | Effect |
|---|---|---|
RUST_LOG | info | what is written, as tracing-subscriber’s EnvFilter reads it |
RIND_LOG | text | json: one JSON object per line |
RIND_SLOW_QUERY_MS | 500 | the 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.