# Add Request ID Logging to a Symfony App

Yes. An agent adds a Monolog processor that puts a request id on every log line, echoes it as X-Request-Id, tests it with WebTestCase and opens a PR.

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

## What the agent does

The agent reads how logging is wired before it adds to it. In an app shaped like symfony/demo, `composer.json` requires PHP 8.4 with `symfony/framework-bundle` 8 and `symfony/monolog-bundle` 4, and `config/packages/monolog.yaml` already does most of the work: in `dev` a stream handler writes `var/log/dev.log`, in `test` a `fingers_crossed` handler buffers until an error, and in `prod` a `fingers_crossed` handler feeds a nested stream on `php://stderr` with `formatter: monolog.formatter.json`. What is missing is any way to tie the lines of one request together. `config/services.yaml` autowires and autoconfigures everything under `src/`, `src/EventSubscriber` holds four subscribers that show the house style, and `src/Command/DeleteUserCommand.php` injects `LoggerInterface` and logs the deletion, so console commands need an id too. `composer.lock` has no `symfony/uid`, so the agent generates ids without adding a package.

It adds `src/Monolog/RequestIdProcessor.php`, a class implementing Monolog's `ProcessorInterface` whose `__invoke(LogRecord $record)` copies the current id into `$record->extra['request_id']`. Autoconfiguration tags any `ProcessorInterface` service as a `monolog.processor`, so every handler in every environment picks it up with no YAML change. A new `src/EventSubscriber/RequestIdSubscriber.php` listens on `kernel.request` with a high priority, reads an incoming `X-Request-Id` header or makes one with `bin2hex(random_bytes(16))`, stores it on the request attributes and hands it to the processor, then on `kernel.response` sets the same `X-Request-Id` header on the response. A `console.command` listener gives each command invocation its own id, so the line from `app:delete-user` carries one as well.

In `prod` the JSON formatter serialises `extra`, so `request_id` appears as a field in each stderr line with no formatter change; in `dev` Monolog's default line format already prints `%extra%`, so `var/log/dev.log` shows it too. Tests follow `tests/Controller/DefaultControllerTest.php`: a new `tests/EventSubscriber/RequestIdSubscriberTest.php` extends `WebTestCase`, requests `/en/blog/` with an `HTTP_X_REQUEST_ID` server parameter and asserts the response header matches, requests it again without one and asserts a 32-character hex header, and a unit test calls the processor on a `LogRecord` and reads `extra['request_id']`.

Before pushing, the agent runs `vendor/bin/phpunit`, which `phpunit.dist.xml` configures to fail on any deprecation, notice or warning, `vendor/bin/php-cs-fixer check --diff` with the repository's `.php-cs-fixer.dist.php`, `vendor/bin/phpstan` against `phpstan.dist.neon`, and `bin/console lint:container`, the same linters `.github/workflows/lint.yaml` runs. It then starts the app with `APP_ENV=prod`, requests a page and pastes one JSON line from stderr with its `request_id` in the pull request.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add a request id to every log line of this Symfony app and open a pull request. Read composer.json, config/packages/monolog.yaml, config/services.yaml, src/Kernel.php, the subscribers in src/EventSubscriber, src/Command/DeleteUserCommand.php, tests/Controller/DefaultControllerTest.php, phpunit.dist.xml, .php-cs-fixer.dist.php and .github/workflows/lint.yaml and tests.yaml first. Add src/Monolog/RequestIdProcessor.php implementing Monolog's ProcessorInterface and adding extra.request_id to each record, relying on autoconfiguration for the monolog.processor tag. Add src/EventSubscriber/RequestIdSubscriber.php that on kernel.request reads X-Request-Id or generates bin2hex(random_bytes(16)), stores it on the request attributes and in the processor, and on kernel.response sets X-Request-Id on the response; give console commands an id on console.command. Do not add symfony/uid or any package. Add tests/EventSubscriber/RequestIdSubscriberTest.php with WebTestCase: a supplied id is echoed, a missing one yields a 32-character hex id, and a unit test of the processor on a LogRecord. Run vendor/bin/phpunit, vendor/bin/php-cs-fixer check --diff, vendor/bin/phpstan and bin/console lint:container, then run the app with APP_ENV=prod and paste one JSON log line showing request_id plus the test summary in the pull request. Do not change monolog.yaml handlers.",
  "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 two new classes under `src/`, one new test file and no change to `composer.json` or `monolog.yaml`. The description shows one `prod` JSON line from stderr with `extra.request_id`, one `dev` line from `var/log/dev.log` with the same field, and the PHPUnit summary with the three new tests.

On a repository with the demo's `Tests` and `Lint` workflows, the pull request runs PHPUnit on PHP 8.4 and 8.5 across Linux, macOS and Windows, then php-cs-fixer, the YAML, Twig, XLIFF and container linters and `composer audit`, so a deprecation the new subscriber triggers or a style slip fails there.

A reviewer checks that the processor is registered once and not per handler, that a caller-supplied id is kept rather than replaced, that console commands get an id without a request, and that the header is set on error responses too. Then they map `request_id` to a field in their log search and put the header on the reverse proxy.

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

A team bringing several Symfony apps into one log search starts a `POST /o/{org}/sessions` per repository with this prompt and that repository in `resources`. Each run reads its own `monolog.yaml`, so an app still on the line formatter in production gets the JSON formatter proposed alongside the processor, and one that already has `symfony/uid` gets a `Uuid::v7()` instead of `random_bytes`; each run opens its own pull request.

Set a `webhook_url` on the key and each `run.completed` names the `session_url`, from which the pull request link is read into whatever tracks the migration.

## Why run it on Rush?

A Monolog processor is ten lines, but whether it fires in every environment, survives the `fingers_crossed` buffer and reaches console commands is only known by running the app in each mode and reading the lines. In a Rush sandbox the agent runs the test suite, boots the app under `APP_ENV=prod` and `dev`, requests pages with `curl` and keeps the lines it read in the run's record next to the pull request.

The demo's linters are strict, and `phpunit.dist.xml` fails on any deprecation, so a change that passes on one laptop's PHP can fail in CI; the sandbox runs the same `php-cs-fixer`, `phpstan` and `lint:container` commands before the pull request opens. The same request applies to every Symfony app a team runs.

## Frequently asked questions

### Does this need symfony/uid?

No. `composer.lock` does not have it, and `bin2hex(random_bytes(16))` gives a 32-character id with no dependency. If your app already has `symfony/uid`, a `Uuid::v7()` is a one-line swap in the subscriber.

### Why a processor instead of adding the id to each log call?

A processor runs on every record from every channel, including Doctrine, security and the framework's own lines, so the id appears on lines the application code never wrote. Adding it by hand would cover only `src/`.

### Will it show in the fingers_crossed handler's buffered lines?

Yes. The processor runs when the record is created, before the handler buffers it, so when an error flushes the buffer every earlier line of that request already carries the same `request_id`.

### What about commands and message handlers?

The `console.command` listener assigns an id per command run. A Messenger handler that runs outside a request would need the same treatment on `WorkerMessageReceivedEvent`, which the pull request notes as a follow-up since the demo does not use Messenger.

## More Symfony jobs

- [Add Security Headers to a Symfony App](https://getrush.ai/run/add-security-headers/symfony/)
- [Add an XML Sitemap to a Symfony App](https://getrush.ai/run/add-sitemap/symfony/)
- [Add Two-Factor Authentication to a Symfony App](https://getrush.ai/run/add-two-factor-auth/symfony/)

## The same job on other stacks

- [Add Request ID Logging to a tRPC API](https://getrush.ai/run/add-request-id-logging/trpc/)
- [Add Request ID Logging to a Vapor App](https://getrush.ai/run/add-request-id-logging/vapor/)
- [Add Request ID Logging to a Wagtail Site](https://getrush.ai/run/add-request-id-logging/wagtail/)

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