skip to content

You lead a Rust payments workspace of async services and batch jobs: do you standardise on `log` or `tracing`, and what structured-logging rules do you set for every crate?

level: principalimportance: should knowfreq 25%

answer

  1. concurrency needs context
  2. fields, not format strings
  3. the bridge loses structure
  4. libraries emit, binaries collect

basics

~20 s

For async services, standardise on tracing: spans keep per-request context across .await and fields stay structured. Keep log for small synchronous tools. Rules: libraries emit and never install, one subscriber per binary, named fields, secrets skipped, static level caps only in binaries.

solid answer

~40 s

With concurrent requests interleaving on a few worker threads, a flat log line cannot say which payment it belongs to unless every call repeats the context; `tracing` spans carry `order_id` once and attach it to every event inside, across `.await`. So I would standardise on `tracing` for the services, accepting the extra dependencies and the `syn`-based `#[instrument]` macro, and allow `log` in small synchronous tools where the cost buys nothing. The rules: libraries depend on `tracing` (or `log`) and never install a subscriber or enable static max-level or `log-always` features; each binary builds one shared subscriber setup with JSON in production; context goes in named fields (`order_id = %id`), never only in the message; handlers use `skip_all` or explicit `skip` for credentials; dependencies on `log` come in through `LogTracer`, knowing their key-values are dropped.

code

rust · 5 lines
rust
fn main() {
    tracing_subscriber::fmt().with_ansi(false).without_time().init();
    let order_id = 42;
    log::info!(order_id, amount_cents = 1999; "charging card");
}

go deeper

for a junior

Know that log gives records and tracing adds spans that group the events of one request, and that a team picks one for its own code.

for a middle

Explain how spans carry context through async code, how LogTracer brings log records into tracing, and why libraries never install a logger or subscriber.

for a senior

Show the operational cost of mixing facades: key-values lost at the LogTracer bridge, duplicate records with log-always, and static caps leaking through feature unification.

for a principal

Weigh context and structure against dependencies and migration effort, then codify it: facade per crate kind, one shared subscriber setup, field names, secret handling, level meanings.

## What the decision is really about Both crates are **facades**: library code calls macros, and one implementation chosen by the binary decides where records go. The difference is the data model. | | `log` | `tracing` | |---|---|---| | Unit of data | a record at one moment | events plus **spans** with a start and end | | Context | whatever the message repeats | inherited from the current span, across `.await` | | Structured values | key-values behind the `kv` feature | typed fields on every span and event | | Collector | one `log::Log` logger | a `Subscriber`, usually a `Registry` with layers | | Cost to adopt | one small crate | `tracing`, `tracing-core`, `tracing-subscriber`; `#[instrument]` pulls in `syn` | ## Why async services push towards `tracing` A payment service handles many requests at once, and an async runtime interleaves them on a handful of threads. With `log`, the line "gateway slow" says nothing about which order it belongs to unless every call site repeats `order_id`. A **span** such as `charge{order_id=ord_42}` states the context once; every event inside it, including events from dependencies bridged through `LogTracer`, is reported within that span, and `Instrument` keeps that true across `.await`. That attribution is what you need when you are reading a production incident. The arguments for staying on `log` are real but narrow: - a small synchronous CLI or batch tool, where nothing interleaves and context fits in the message; - a library whose users are known to run plain `log` loggers (though `tracing`'s optional `log` feature can emit `log` records when no tracing subscriber is installed); - minimal dependencies and build time, since `#[instrument]` depends on `syn`. ## The structure trap at the boundary Mixing the two is where a standard earns its keep. `log` 0.4 has structured key-values (`log::info!(order_id; "charging card")` with the `kv` feature, which `env_logger` prints when its own `kv` feature is on). But **tracing-log 0.2's `LogTracer` drops them**: through the bridge only the message text arrives. ```plaintext INFO bridge: charging card ``` A team that half-migrates gets the worst of both: services on `tracing`, a shared crate still on `log` with key-values, and the identifiers vanishing at the boundary. Picking one facade for the workspace's own crates prevents this. ## The rules a lead writes down 1. **Libraries emit, binaries collect.** Library crates depend on the facade only, never call `set_global_default` or a logger's `init()`, and never enable `max_level_*`, `release_max_level_*` or `log-always`. Cargo unifies features across the build, so one library's static cap applies to everyone; the `log` docs warn libraries off those features for exactly that reason. 2. **One subscriber per binary.** A shared setup function builds the registry, the layers and their filters, with JSON output in production and readable output locally. 3. **Context in fields.** Identifiers go in named fields with agreed names (`order_id`, `merchant_id`), recorded with `%` or `?` deliberately, never only interpolated into the message. 4. **Spans at boundaries.** Each request, job run and outbound call gets a span, via `#[instrument]` or `.instrument(span)`; holding `span.enter()` across `.await` is a review failure. 5. **No secrets.** Handlers receiving card data, tokens or keys use `skip_all` with explicit `fields`, because `#[instrument]` records every argument by default. 6. **Level meanings.** `ERROR` means someone must act, `WARN` means degraded, `INFO` is the business trail, `DEBUG` and `TRACE` are off in production unless a filter enables them for an investigation. ## Rolling it out 1. Write the shared subscriber setup and switch every binary to it, deleting its `env_logger` initialisation in the same change, since both would claim the `log` slot; existing `log` call sites keep printing through `LogTracer`. 2. Add spans at the request, job and outbound-call boundaries, which delivers most of the value before any line is rewritten. 3. Convert the workspace's own crates from `log` to `tracing` as they are touched, moving identifiers out of message text into fields. 4. Keep the `log` bridge for third-party dependencies that still use `log`, and re-check them when they release a `tracing` version. ## What to weigh before committing - **Migration cost**: `log` call sites keep working through `LogTracer`, so the move can be gradual, crate by crate. - **Performance**: both facades check a static and a global level before doing work; field formatting happens only when something is enabled. - **Where data goes next**: exporters, metrics and distributed-tracing backends attach as more layers, and they are covered with observability; the decision here is only which facade the code speaks.

  • A shared domain crate must stay usable by an older binary that only installs `env_logger`. Which facade does it use?
    It can still use `tracing` and enable `tracing`'s `log` feature: when no tracing subscriber is installed, its events are emitted as `log` records the old binary's `env_logger` prints, while the new services see full spans and fields. It must not enable `log-always`, which would duplicate records in binaries that bridge `log` into `tracing`.
  • Would you use `release_max_level_info` to make production builds cheaper?
    Only in the binary's own manifest, and only after measuring. It compiles out `DEBUG` and `TRACE` call sites, so they cannot be re-enabled for an incident without a rebuild. Because Cargo features unify, a library enabling it would impose that loss on every consumer, so the rule is that only binaries set static caps.

  • log and tracing are interchangeable; the choice does not matter for async code.
  • The log crate cannot carry key-value fields at all.
  • Bridging log into tracing keeps log's key-values as tracing fields.
  • A shared library should enable release_max_level_info to speed up production.
  • Every crate should install its own subscriber so its output is never lost.