#Add a trace id to Symfony logs with Monolog and OpenTelemetry
Let's say our Symfony shop logs a warning when a query is slow. In the morning the log has forty of them. We want to open one and see the whole request: the route, the other queries, the time each step took.
These steps need a Symfony application and a collector, so they run only on your machine, not in the playground.
#Add a request id to Monolog by hand
The first idea is always a Monolog processor that puts a random id on every record of one request.
<?php
declare(strict_types=1);
namespace App\Logging;
use Monolog\Attribute\AsMonologProcessor;
use Monolog\LogRecord;
#[AsMonologProcessor]
final class RequestIdProcessor
{
private readonly string $requestId;
public function __construct()
{
$this->requestId = bin2hex(random_bytes(4));
}
public function __invoke(LogRecord $record): LogRecord
{
$record->extra['request_id'] = $this->requestId;
return $record;
}
}
[2026-12-01T21:43:36.280622+00:00] app.WARNING: slow query {"route":"prices","duration":279.918708} {"request_id":"fad06b4d"}
It works: all lines of one request have the same id, and we can search for it.
#Why a request id is not enough
The id connects log lines with other log lines, and with nothing else.
- It doesn't lead to the timeline of the request, because the trace has another id.
- A worker that handles a message of this request makes its own id.
- The next service we call knows nothing about it.
Tracing already has an id for one piece of work, and it travels to workers and to other services. The log lines only have to use that one.
#Send Monolog records to OpenTelemetry
flow-php/telemetry is an independent implementation of OpenTelemetry, written in PHP, and it is not a wrapper around the official SDK. The bundle needs PHP 8.3 or newer and Symfony 6.4, 7.4 or 8.0. The only PHP extension it needs is curl.
composer require flow-php/symfony-telemetry-bundle flow-php/telemetry-otlp-bridge flow-php/monolog-telemetry-bridge
The guide about Symfony monitoring with OpenTelemetry shows the installation of the bundle step by step. For logs it gets a logger_provider:
# config/packages/flow_telemetry.yaml
flow_telemetry:
resource:
custom:
service.name: shop
exporters:
collector:
otlp:
transport:
type: curl
endpoint: 'http://localhost:4318'
timeout_ms: 200
connect_timeout_ms: 50
tracer_provider:
processor:
type: batching
exporter: collector
logger_provider:
processor:
type: batching
exporter: collector
instrumentation:
http_kernel:
enabled: true
dbal:
enabled: true
Monolog stays the logger of the application. We give it one more handler, which passes each record to the telemetry logger:
# config/services.yaml
services:
app.telemetry_logger:
class: Flow\Telemetry\Logger\Logger
factory: ['@Flow\Telemetry\Telemetry', 'logger']
arguments: ['app']
Flow\Bridge\Monolog\Telemetry\TelemetryHandler:
arguments: ['@app.telemetry_logger']
# config/packages/monolog.yaml
monolog:
handlers:
telemetry:
type: service
id: Flow\Bridge\Monolog\Telemetry\TelemetryHandler
channels: ["app"]
We remove RequestIdProcessor. The controllers and services keep their LoggerInterface, and no log call changes.
#One log line, one trace
We open the slow page again. The log file has its line as before, and the collector gets the same record with two more fields:
LogRecord #0
SeverityText: WARN
Body: Str(slow query)
-> monolog.channel: Str(app)
-> monolog.level_name: Str(Warning)
-> context.route: Str(prices)
-> context.duration: Double(260.913792)
Trace ID: f1a96ab0d6ac300d22f4fa6af33a71b8
Span ID: b2ae0eb0620a5192
Trace ID is the id of the request that wrote the line, and Span ID is the step that was running at that moment.
Nobody passed these ids to the logger. The handler found the span that was open when the line was written.
In a monitoring tool we click the warning and land in the trace of its request, with the route, the SQL query of 260ms and everything around it. From the trace we see all its log lines under the timeline.
The context of Monolog is kept: route and duration are attributes of the record, so we can filter on them.
#Which Monolog channels to send
channels: ["app"] sends the records of our own code. That is a good start, because the channels of the framework are loud.
- To add a channel, put it into the list:
["app", "security"]. - To send only warnings and errors, give the handler a level, the same way as for every Monolog handler.
- The file handler stays as it is. When the collector is down, the lines are still in the file.
#Where to go from here
A message handled by a worker keeps the connection to its request: see Symfony Messenger monitoring. The same idea without Symfony is in PHP logs with a trace id. All the options of the logger provider are in the bundle documentation.