Configure logs in context for PHP

Logs in context for the PHP agent connects your logs and APM data in New Relic, giving full context to high-level events and providing high value data to specific log lines. Read on to learn how to configure logs in context and start getting log data.

Compatibility and requirements

To use log management with the PHP agent, ensure your configuration meets the following requirements:

Configure logs in context with log management

To configure logs in context with PHP:

  1. Enable log management.
  2. Install or update the PHP agent.
  3. Configure the Monolog extension.
  4. Optional: Advanced configuration.
  5. Check for logging data.

Enable log management

Confirm that you have log management enabled.

Install or update the PHP agent

Install or update to the most recent PHP agent version, and enable distributed tracing.

Configure logs in context for PHP

Install the Monolog log enricher

You will need to install the Monolog log enricher package before you can configure Monolog for New Relic logs in context. This is done by using Composer to install the newrelic/monolog-enricher package.

Add the Monolog handler

In most cases, you should use the Processor and Handler classes provided by the log enricher package in conjunction with Monolog's built-in BufferHandler. The Processor adds the contextual metadata required for New Relic logs in context to operate, the Handler sends the log records to New Relic Logs, and the BufferHandler ensures that the logs are delivered with as little overhead as possible.

At the point where you create the Monolog\Logger object, you should add the following pushProcessor and pushHandler calls to add the Processor and Handler:

<?php

use Monolog\Handler\BufferHandler;
use Monolog\Logger;
use NewRelic\Monolog\Enricher\{Handler, Processor};

$log = new Logger('log');
$log->pushProcessor(new Processor);
$log->pushHandler(new BufferHandler(new Handler));

You can use the Processor and Handler in conjunction with any existing Monolog setup: you do not need to remove your existing processors and handlers.

Advanced configuration

Other logging tools

If you use Monolog to send logs to a compatible logging tool, and have already configured that tool to send logs to New Relic Logs, then you may prefer to use the Processor and Formatter to solely use that tool, instead of incurring the additional overhead of sending logs directly to New Relic with the Handler.

You will need to be using a handler that supports the setFormatter() method, and your logging tool must be able to ingest JSON logs.

An example of configuring Monolog to handle this is shown below:

<?php

use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use NewRelic\Monolog\Enricher\{Formatter, Processor};

$log = new Logger('log');
$log->pushProcessor(new Processor);

$handler = new StreamHandler('php://stderr');
$handler->setFormatter(new Formatter);
$log->pushHandler($handler);

Check for logging data

If you have configured your logging as described above, run your application locally, ensure one or more log records are created, and check the New Relic Logs UI. You can find them using a query like this:

service.name:"your_app_name" has:trace.id

Troubleshooting

If the logs from your application do not include fields like trace.id and span.id, there may be a problem with the configuration of the Monolog log enricher.

Check that:

  • the application is using the Monolog logger object when sending log records, and not syslog().
  • the PHP agent has been installed, and is configured to enable distributed tracing.
  • another Monolog handler is not preventing the log record from bubbling.

What's next?

Now that you've set up APM logs in context, here are some potential next steps:

For more help