# Add Request ID Logging to a Leptos App on Axum

Yes. An agent adds a request id to every request of your Leptos server on Axum with tower-http, logs each request through tracing with that id, and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-request-id-logging/leptos/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads the server half of the app, which in a Leptos project is a feature-gated corner of one crate. In a project generated from leptos-rs/start-axum, `Cargo.toml` declares `leptos`, `leptos_router` and `leptos_meta` at 0.8 for both targets, and `axum` 0.8, `tokio`, `leptos_axum` and `console_error_panic_hook` as optional dependencies pulled in by the `ssr` and `hydrate` features, because the same crate compiles to WebAssembly for the browser and to a native binary for the server. `src/main.rs` is wrapped in `#[cfg(feature = "ssr")]`: it reads the Leptos config, generates the route list from `App`, builds an `axum::Router` with `leptos_routes` and a `file_and_error_handler` fallback, prints one line with `leptos::logging::log!` and calls `axum::serve`. There is no `tower-http`, no `tracing`, no middleware layer and no per-request log line at all; a request that panics or hangs leaves nothing to search for. `src/app.rs` holds the `shell` and the `App` component and needs no change.

The change is three layers on the Router and two new optional dependencies. The agent adds `tower-http` 0.7 with its `request-id` and `trace` features, `tracing` and `tracing-subscriber` with `env-filter`, all `optional = true` and all listed under the `ssr` feature, so the WebAssembly build never sees them. In `main.rs` the Router gains, in this order, `PropagateRequestIdLayer::x_request_id()` so the id is copied onto the response, a `TraceLayer::new_for_http()` whose `make_span_with` reads the `x-request-id` header into an `info_span!` with the method and path, and `SetRequestIdLayer::x_request_id(MakeRequestUuid)` as the outermost layer. The order matters: Axum applies the last `.layer` call first, so the id is set before the span is made and propagated after the handler runs. A client that sends its own `x-request-id` keeps it; one that does not gets a UUID v4.

Logging replaces the one `log!` line with `tracing_subscriber::fmt()` initialised from `RUST_LOG`, defaulting to `info` for the crate and `tower_http=info`, so the trace layer's `on_response` line, with status and latency, carries the request id from its span. Server functions get the id too: a `#[server]` function can call `leptos_axum::extract` for the request headers and read `x-request-id`, and the agent adds one example in a comment rather than inventing a server function the app does not have.

The check is a build and a request. The agent runs `cargo leptos build`, which compiles both targets and would fail if a server-only crate leaked into the `hydrate` build, then starts the binary and sends two requests with `curl -i`: one bare, to see a generated id come back in the response header and appear in the log line, and one with `x-request-id: test-123`, to see the same value echoed. It also runs `cargo leptos end-to-end`, the Playwright test under `end2end/`, so the rendered page is unchanged. The pull request quotes both curl exchanges and the two log lines.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add request id logging to this Leptos app's Axum server. Read Cargo.toml, src/main.rs, src/app.rs, src/lib.rs and end2end/ first. Add tower-http with the request-id and trace features, tracing, and tracing-subscriber with env-filter as optional dependencies enabled only by the ssr feature. In src/main.rs add SetRequestIdLayer with MakeRequestUuid as the outermost layer, a TraceLayer whose make_span_with puts the x-request-id header, method and path into an info span, and PropagateRequestIdLayer so the id is returned on the response. Replace the leptos logging line with a tracing-subscriber fmt subscriber read from RUST_LOG, defaulting to info for the crate and tower_http. Do not add server functions; leave a comment showing how one would read the header through leptos_axum::extract. Run cargo leptos build, start the server, send one request without an id and one with x-request-id set, and confirm the header on both responses and in the log lines. Run cargo leptos end-to-end. Open a pull request that quotes the curl output and the log lines.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

The pull request changes `Cargo.toml` (three optional dependencies and the `ssr` feature list) and `src/main.rs` (the subscriber and three layers), and touches no component code. Its description quotes the two `curl -i` exchanges with their `x-request-id` headers, the matching log lines with method, path, status and latency, and the `cargo leptos build` and end-to-end output.

A reviewer checks that every new dependency is optional and listed under `ssr`, that the layer order puts `SetRequestIdLayer` outermost, and that the subscriber's default filter is what they want in production. They may prefer JSON output for a log collector, which is one feature flag on `tracing-subscriber` and one method call on the builder.

In production, every log line for a request carries the same id, the id is on the response for the client to report, and an id a load balancer or client sets upstream is kept rather than replaced.

## Run it on every new Leptos service

A handler on the reader's own server watches for new repositories whose `Cargo.toml` names `leptos_axum` and sends this body once per repository to `POST /o/{org}/sessions`, naming the repository in `resources` and a `request_id` that is a UUID v5 of the repository name so a retried call returns the run it already started. Each run reads that project's own `main.rs`, so a service that already has a `TraceLayer` gets only the id layers added around it, and one on Actix rather than Axum gets the equivalent middleware for its framework.

An automation on the Project with a `github` trigger on `pull_request.opened` and `pull_request.synchronize` reads pull requests that touch `src/main.rs`. An automation's run is read-only, so when a change reorders the layers or adds a server-only crate outside the `ssr` feature, it leaves a review comment with the line to fix and pushes nothing.

## Why run it on Rush?

A Leptos crate compiles twice, and a dependency added in the wrong place breaks the browser build with an error that names a crate the app never imports directly. The agent makes the change, runs the full `cargo leptos build` for both targets in its own sandbox, starts the server and shows the id travelling from request to log line to response, with every command and its output kept beside the pull request.

Request ids are the first thing a team wants in every service once one incident has gone unexplained. The same request runs over every Rust service at once, each in its own sandbox, each getting the layers that fit its framework and its existing logging rather than one snippet pasted everywhere.

## Frequently asked questions

### Why must the new crates be optional?

The same crate builds to WebAssembly for the `hydrate` feature, and `tower-http`, `tracing-subscriber` and their dependencies do not belong in that target. Listing them under the `ssr` feature keeps the browser bundle unchanged, and `cargo leptos build` fails loudly if one leaks.

### What if a client already sends x-request-id?

`SetRequestIdLayer` only sets the header when it is missing, so an id from a load balancer or a calling service is kept, logged and returned. A bare request gets a fresh UUID v4 from `MakeRequestUuid`.

### Why that layer order?

Axum wraps layers so the last `.layer` call runs first on the way in. `SetRequestIdLayer` last in the source means it runs first, so the span the `TraceLayer` makes already sees the header, and `PropagateRequestIdLayer`, innermost, copies the id onto the response on the way out.

### Can a server function read the id?

Yes. Inside a `#[server]` function, `leptos_axum::extract` gives the request parts, and the `x-request-id` header is there. The pull request leaves that as a comment next to the layers rather than adding a server function the app does not have.

### Does this change what the browser renders?

No. The layers sit around the Router, the components in `src/app.rs` are untouched, and the Playwright test under `end2end/` runs as before. The only visible difference is one extra response header.

## More Leptos jobs

- [Add Security Headers to a Leptos Axum App](https://getrush.ai/run/add-security-headers/leptos/)
- [Add Structured Logging to a Leptos Axum App](https://getrush.ai/run/add-structured-logging/leptos/)
- [Add Tailwind CSS to a Leptos App](https://getrush.ai/run/add-tailwind/leptos/)

## The same job on other stacks

- [Add Request ID Logging to a Litestar App](https://getrush.ai/run/add-request-id-logging/litestar/)
- [Add Request ID Logging to an Express and Mongoose API](https://getrush.ai/run/add-request-id-logging/mongoose/)
- [Add Request ID Logging to a NestJS API](https://getrush.ai/run/add-request-id-logging/nestjs/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
