Skip to content

Structured logs only

For a program that wants good log lines and nothing else: a command-line tool, a service whose platform ships its standard error to a log store, or a first step before traces.

examples/logs.rs
fn main() -> Result<(), oxtel::Error> {
// Text on a terminal, JSON anywhere else. Keep the guard alive.
let _logs = oxtel::init()?;
// Fields are structured: in JSON, each is a key of its own.
tracing::info!(user.id = 42, plan = "team", "signed in");
// Lines inside a span carry its fields and its trace id, so every line
// about one request can be found together.
let request = tracing::info_span!("request", http.route = "/orders", order.id = 7);
let _entered = request.enter();
tracing::warn!(attempt = 2, "payment provider slow; retrying");
Ok(())
}
Cargo.toml
[dependencies]
oxtel = "0.3"
tracing = "0.1"

With these, the same program sends traces, metrics and logs to a collector the day OTEL_EXPORTER_OTLP_ENDPOINT is set, with no code changes. If it will only ever want logs, leave oxtel’s export code out, and every HTTP and TLS crate with it:

Cargo.toml
[dependencies]
oxtel = { version = "0.3", default-features = false }
tracing = "0.1"

From a checkout of the repository:

Terminal window
cargo run -p oxtel-examples --example logs # text, on a terminal
cargo run -p oxtel-examples --example logs 2>&1 | cat # JSON, since that isn't one
OXTEL_LOG_FORMAT=ecs cargo run -p oxtel-examples --example logs

Everything here is set from the environment, so it can change without a rebuild:

SettingWhat it does
RUST_LOGhow much is logged: info (the default), debug, warn, or per module, such as info,hyper=warn
OXTEL_LOG_FORMATauto (the default: text on a terminal, JSON otherwise), text, json, ecs (Elastic Common Schema) or none
OXTEL_LOG_OUTPUTstderr (the default) or stdout
  • JSON is already the default anywhere that isn’t a terminal, such as a container or a service manager.
  • Keep the value oxtel::init() returns until main ends. Log lines are written as they happen, so for them it makes no difference; once you send telemetry somewhere, dropping it at the end of main is what sends the last batch.