# Add Request ID Logging to a Vapor App

Yes. An agent adds a middleware that returns Vapor's request id as X-Request-Id and puts it in every access log line, tests it with VaporTesting and opens a PR.

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

## What the agent does

The agent starts from what Vapor already does, because a request id exists before any code is written. In a project generated from vapor/template, `Package.swift` depends on `vapor` from `4.121.4`, optionally `fluent 4.13.0` and `leaf 4.5.1`, and `swift-nio 2.101.0`, with a test target on `VaporTesting`. `entrypoint.swift` calls `LoggingSystem.bootstrap(from: &env)` and `Application.make(env)` before `configure`, and `configure.swift` registers the database, the migrations and `routes(app)`; the default middleware stack is untouched. `routes.swift` serves `GET /`, `GET /hello` and the `TodoController` at `/todos`. In Vapor's `Request`, the `id` is the value of the incoming `X-Request-Id` header when present and a fresh UUID string otherwise, and the request logger carries it as `request-id` metadata. Two things are missing: the id never reaches the response, and Vapor's documentation says logger metadata only shows at debug level or lower, so in production at `info` the id is in no line.

The agent adds `Sources/App/Middleware/RequestIDMiddleware.swift`, an `AsyncMiddleware` whose `respond` calls the next responder, sets `X-Request-Id` on the response from `request.id`, and logs one line through `request.logger` at `info` with the method, the path, the status and the id in the message text itself rather than only in metadata, so it survives the production log level. `configure.swift` registers it with `app.middleware.use(RequestIDMiddleware())` before `routes(app)`. Because `Request.id` already honours an incoming `X-Request-Id`, a caller or a load balancer that sends one sees it echoed, and a caller that sends none gets the UUID Vapor generated; no header parsing is written twice.

The template's tests use the `Testing` framework: `AppTests.swift` opens a `@Suite`, builds the app in `withApp`, and asserts `GET hello` answers `Hello, world!` with `app.testing().test(.GET, ...)`. The agent adds `RequestIDTests.swift` in the same style: `GET /hello` answers with a non-empty `X-Request-Id`; a request sent with `X-Request-Id: test-abc-123` gets the same value back; two plain requests get different values; and a `404` from an unknown path still carries the header, because the middleware sits outside the router. `Package.swift` needs no new dependency.

Then it proves it. The sandbox runs `swift build` and `swift test`, using the `swift:6.3-noble` image the `Dockerfile` names when the sandbox has no matching toolchain, and the agent pastes both outputs. It starts the server with `swift run` and sends one request with `curl -i` to show the header in a real response and the access line in the console, and notes that `docker-compose.yml` sets `LOG_LEVEL` to `debug` by default, where the metadata form is also visible.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add request id logging to this Vapor application and open a pull request. Read Package.swift, Sources/App/entrypoint.swift, configure.swift, routes.swift, the controllers, Tests/AppTests and docker-compose.yml first. Vapor's Request already has an id taken from an incoming X-Request-Id header or generated as a UUID, and the request logger carries it as request-id metadata; do not reimplement that. Write Sources/App/Middleware/RequestIDMiddleware.swift as an AsyncMiddleware that sets X-Request-Id on every response from request.id and logs one info line per request through request.logger with the method, path, status and the id in the message text, and register it in configure.swift before routes. Add tests in the same Testing style as the existing suite: a response carries a non-empty X-Request-Id, a request that sends X-Request-Id gets the same value back, two requests get different ids, and a 404 still carries the header. Run swift build and swift test, start the app and show one curl -i response and its log line, and paste all of it in the pull request. Add no dependency.",
  "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

What comes back is a pull request with one new file under `Sources/App/Middleware/`, one `app.middleware.use` line in `configure.swift` and one new test file beside `AppTests.swift`. The description shows a `curl -i` response with its `X-Request-Id`, the matching log line with the same id, and the output of `swift build` and `swift test` from the sandbox.

The template repository's workflow, `test-template.yml`, generates projects from the template and builds them, which is not the check a generated project carries; on a project with its own Swift workflow the new tests run on the pull request. Nothing in `Package.swift` changes, so dependency resolution is unaffected.

A reviewer checks that the header is set on error responses too, that the log line is at `info` and includes the id in its text rather than only as metadata, and that an incoming `X-Request-Id` is echoed rather than replaced, which matters if a load balancer in front already assigns one. Then they merge and look for the id in the next production log.

## Run it on every service onboarded to the log pipeline

A team moving its Swift services onto one log pipeline sends one `POST /o/{org}/sessions` per repository with this prompt and the repository in `resources`. Each run reads its own `configure.swift`, so a service that already sets a custom log handler keeps it and gets the header middleware only, and each run opens its own pull request.

Give the API key a `webhook_url` and Rush POSTs `run.completed` when each turn ends, with `session_url` to read the pull request from, so the header name can be checked against what the gateway expects before merging.

## Why run it on Rush?

The easy mistake here is doing work Vapor has already done: parsing `X-Request-Id`, generating an id, attaching it to the logger. The agent reads Vapor's own `Request` first and adds only what is missing, the response header and an info-level line, which keeps the change small and the behaviour consistent with the id the framework already logs at debug.

In a Rush sandbox the proof is a real server: the agent builds with the toolchain the `Dockerfile` names, runs the test target, starts the app and sends a request, and the response headers and the console line are kept in the run's record. The same request works unchanged on the next Vapor service.

## Frequently asked questions

### Does Vapor already have a request id?

Yes. `Request.id` is the incoming `X-Request-Id` when the caller sends one and a UUID string otherwise, and the request logger carries it as `request-id` metadata. What the change adds is the id on the response and in an info-level line, since metadata only shows at debug level.

### Why log the id in the message text?

Because Vapor's documentation says logger metadata is only shown at debug level or lower, and production usually runs at `info`. Putting the id in the text keeps it visible at that level without lowering the level everywhere.

### Will it respect ids from a load balancer?

Yes. The middleware reads `request.id`, which Vapor sets from the incoming `X-Request-Id` header when present, so an id assigned upstream is echoed back and logged rather than replaced. The test that sends `X-Request-Id` checks that.

### Does it work without Fluent or Leaf?

Yes. The middleware touches only the request, the response and the logger, so it is the same in a project generated with or without Fluent and Leaf. The tests use the plain `withApp` helper the template's test suite already has.

## More Vapor jobs

- [Add Security Headers to a Vapor App](https://getrush.ai/run/add-security-headers/vapor/)
- [Add Seed Data to a Vapor App](https://getrush.ai/run/add-seed-data/vapor/)
- [Add Soft Delete to a Vapor App](https://getrush.ai/run/add-soft-delete/vapor/)

## The same job on other stacks

- [Add Request ID Logging to a Wagtail Site](https://getrush.ai/run/add-request-id-logging/wagtail/)
- [Add a Request ID to Every Winston Log Line in an Express App](https://getrush.ai/run/add-request-id-logging/winston/)
- [Add Request ID Logging to an Actix Web App](https://getrush.ai/run/add-request-id-logging/actix/)

## 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/)
