Documentation forSolarWinds Observability Saas

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.

Application logs for your service entity are not included in the data sent by APM libraries. Configure your server or application to send application logs to SolarWinds Observability. See Add logs from services.

If trace context is added to logs and those logs are sent to SolarWinds Observability, use the Traces Explorer to see the logs associated with a traced transaction.

Monolog

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;
});

log4PHP

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

LoggerNDC

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

LoggerNDC::push(solarwinds_apm_get_log_trace_id(););

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

LoggerMDC

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);

Analog

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();
    $handler($info);
  };
}

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

Analog::handler(solarwindsInit(Analog\Handler\Stderr::init()));

An example passing in the log file handler:

Analog::handler(solarwindsInit(Analog\Handler\File::init('/tmp/myLog.txt')));

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.