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.
The code
Section titled “The code”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(())}Dependencies
Section titled “Dependencies”[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:
[dependencies]oxtel = { version = "0.3", default-features = false }tracing = "0.1"Run it
Section titled “Run it”From a checkout of the repository:
cargo run -p oxtel-examples --example logs # text, on a terminalcargo run -p oxtel-examples --example logs 2>&1 | cat # JSON, since that isn't oneOXTEL_LOG_FORMAT=ecs cargo run -p oxtel-examples --example logsChoices
Section titled “Choices”Everything here is set from the environment, so it can change without a rebuild:
| Setting | What it does |
|---|---|
RUST_LOG | how much is logged: info (the default), debug, warn, or per module, such as info,hyper=warn |
OXTEL_LOG_FORMAT | auto (the default: text on a terminal, JSON otherwise), text, json, ecs (Elastic Common Schema) or none |
OXTEL_LOG_OUTPUT | stderr (the default) or stdout |
For production
Section titled “For production”- 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 untilmainends. Log lines are written as they happen, so for them it makes no difference; once you send telemetry somewhere, dropping it at the end ofmainis what sends the last batch.