#PHP logs with a trace id: connect Monolog and OpenTelemetry
Let's say our nightly import writes a log line for every order it loads, and one line when something goes wrong. In the morning we find an error in the log, and we want every other line of the same order.
Doesn't look like a complicated task, right?
#Log lines without a trace id
The first idea is always to put the order id into every line.
<?php
declare(strict_types=1);
foreach ([1001, 1002] as $orderId) {
echo date('H:i:s'), " INFO order loaded order.id={$orderId}\n";
if ($orderId === 1002) {
echo date('H:i:s'), " ERROR payment missing order.id={$orderId}\n";
}
}
21:38:03 INFO order loaded order.id=1001
21:38:03 INFO order loaded order.id=1002
21:38:03 ERROR payment missing order.id=1002
It works, as long as every line is about an order and every function knows the order id.
#Why an id in the message is not enough
The import calls a payment client, a mailer and a database layer. They log too, and none of them knows our order id. To get it into their lines, we have to pass it through every function, or put it into a global.
And an order id is not the unit we look for. The same order is imported again tomorrow, and both runs have the same id.
What we want is an id for one piece of work, given to every line that is written while that work runs, also by code that doesn't know about it. Tracing already has that id: the trace id.
#Write PHP logs with a trace id
flow-php/telemetry is an independent implementation of OpenTelemetry, written in PHP. It needs PHP 8.3 or newer, no extension, and it is not a wrapper around the official SDK.
composer require flow-php/telemetry flow-php/etl
<?php
declare(strict_types=1);
use function Flow\ETL\DSL\clock;
use function Flow\Telemetry\DSL\{
console_exporter,
logger_provider,
memory_context_storage,
pass_through_log_processor,
pass_through_span_processor,
resource,
telemetry,
tracer_provider,
};
require __DIR__ . '/vendor/autoload.php';
$exporter = console_exporter(colors: false);
$context = memory_context_storage();
$telemetry = telemetry(
resource(['service.name' => 'nightly-import']),
tracer_provider(pass_through_span_processor($exporter), clock(), $context),
loggerProvider: logger_provider(pass_through_log_processor($exporter), clock(), $context),
);
$tracer = $telemetry->tracer('import');
$logger = $telemetry->logger('import');
foreach ([1001, 1002] as $orderId) {
$tracer->trace('import-order', static function () use ($logger, $orderId): void {
$logger->info('order loaded', ['order.id' => $orderId]);
if ($orderId === 1002) {
$logger->error('payment missing', ['order.id' => $orderId]);
}
});
}
The log lines of the output, without the frames around them:
| Timestamp | Level | Message | Trace |
| 2026-12-01 21:37:52.677542 | INFO | order loaded {order.id: 1001} | e726e4e1/41edf58d |
| 2026-12-01 21:37:52.677834 | INFO | order loaded {order.id: 1002} | 481163f1/17468f32 |
| 2026-12-01 21:37:52.677856 | ERROR | payment missing {order.id: 1002} | 481163f1/17468f32 |
The last column is new. It is the start of the trace id and of the span id, and we never passed either of them to the logger.
The error and the line before it have the same trace id, 481163f1. The first order has another one.
It works because the tracer and the logger share one memory_context_storage(). trace puts its span there, and the logger reads it when a line is written. Code deep inside the import gets the same id, with no argument for it.
The script also prints the span of each order:
| SPAN: import-order
| Trace: 481163f1f459eb639ff7497d3809fe0f Span: 17468f32427553a0
So the id in the log line leads to a trace, with the duration and the steps of exactly that run.
#Keep Monolog and add the trace id
This script needs Monolog, so it runs only on your machine, not in the playground.
Most applications already log through Monolog, and nobody wants to rewrite every log call. We don't have to: one more handler sends each Monolog record to the telemetry logger.
composer require flow-php/monolog-telemetry-bridge monolog/monolog
<?php
declare(strict_types=1);
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use function Flow\Bridge\Monolog\Telemetry\DSL\telemetry_handler;
use function Flow\ETL\DSL\clock;
use function Flow\Telemetry\DSL\{
console_exporter,
logger_provider,
memory_context_storage,
pass_through_log_processor,
resource,
telemetry,
tracer_provider,
void_span_processor,
};
require __DIR__ . '/vendor/autoload.php';
$context = memory_context_storage();
$telemetry = telemetry(
resource(['service.name' => 'nightly-import']),
tracer_provider(void_span_processor(), clock(), $context),
loggerProvider: logger_provider(pass_through_log_processor(console_exporter(colors: false)), clock(), $context),
);
$logger = new Logger('import', [
new StreamHandler('php://stderr'),
telemetry_handler($telemetry->logger('import')),
]);
$telemetry->tracer('import')->trace('import-order', static function () use ($logger): void {
$logger->error('payment missing', ['order_id' => 1002]);
});
[2026-12-01T21:38:03.769914+00:00] import.ERROR: payment missing {"order_id":1002} []
| Level | Message | Trace |
| ERROR | payment missing {monolog.channel: import, monolog.level_name: Error, context.order_id: 1002} | b69e7a69/a312a9bc |
The first line is Monolog as before, written by the old handler. The second one is the same record on the telemetry side, with the trace id, the channel and the context of Monolog as attributes. The log calls of the application didn't change.
#Where to go from here
The console is for our machine. On a server the records go over OTLP to a collector, next to the spans, and a monitoring tool shows the lines of one trace under its timeline. Only the exporter changes: see send PHP traces over OTLP.
The guide about tracing in PHP shows where the trace id comes from. When a request goes through more than one service, distributed tracing keeps one id for all of them, and so for all of their log lines. In a Symfony application the bundle does the wiring: see add a trace id to Symfony logs.