Documentation forSolarWinds Observability

PHP trace context in logs

Adding trace context to application logs provides the ability to correlate the log messages from a traced transaction, and if sampled, the log messages to the transaction trace detail.

The SolarWinds Observability PHP Library can automatically append the trace context to internal errors, internal warnings, and PHP logs generated with the error_log function. See Automatic insertion of trace context into logs.

PHP logging frameworks handle log messages different ways. To add trace context to log messages when a PHP logging framework is used, use the SDK function solarwinds_apm_get_log_trace_id(). Below are examples on how to do this for some of the most popular frameworks.


An example capturing the trace context with a monolog_processor is:

$logger->pushProcessor(function ($record) {
  $record['message'] .= solarwinds_apm_get_log_trace_id();
  return $record;


In log4PHP, either the NDC or the MDC Logger can be used to add the trace context to log messages.


An example method to push the SolarWinds Observability trace context to the LoggerNDC library is:


In the config file, use the LoggerLayoutPattern. In the value of the conversion pattern, add %x wherever the trace context should appear.


An example integrating the SolarWinds Observability trace context with the LoggerMDC library is:

LoggerMDC::put("trace", solarwinds_apm_get_log_trace_id(););

In the config file, use the LoggerLayoutPattern. In the value of the conversion pattern, add %X{trace} wherever the trace context should appear.

$context = ['ao.traceId' => solarwinds_apm_get_log_trace_id()];
$logger->error('An error log', $context);


Define an Analog custom handler for trace context logging:

function solarwindsInit($handler) {
  return function ($info) use ($handler) {
    $info["message"] .= solarwinds_apm_get_log_trace_id();

Register this handler with Analog and pass in your preferred handler. A general example for including Trace Context information in Analog is:


An example passing in the log file handler:


The scripts are not supported under any SolarWinds support program or service. The scripts are provided AS IS without warranty of any kind. SolarWinds further disclaims all warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The risk arising out of the use or performance of the scripts and documentation stays with you. In no event shall SolarWinds or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the scripts or documentation.