With our PHP agent, you can get logs in context, which lets you see your app logs in the context of your other New Relic data. For general information on this feature, see APM logs in context.
You have two options to configure logs in context to send your app's logs and linking metadata automatically to New Relic. Supported frameworks for automatic logs in context using in agent forwarding include:
Monolog 2 or 3. Requires PHP Agent v10.1.0+.
The other option is to have the agent decorate log records with a token containing linking metadata and use an external log forwarder to send logs to New Relic. Supported frameworks for log decoration include:
Monolog 2 or 3. Requires PHP Agent v10.13.0+.
If you're using a supported framework, you can configure the APM agent to send your app's logs and linking metadata automatically to New Relic. Supported frameworks for automatic logs in context include:
Monolog (version 2 or 3)
This is the simplest approach, and it's a great choice for developers who may not have the access or interest in setting up a log forwarder, or for accounts that want to see the power of logs and other linking metadata in context of their apps, without a lot of overhead.
As of agent release 10.3.0 the logging metrics and agent log forwarding features are enabled by default.
All you need to do is install an agent version with log forwarding capabilities (PHP agent 10.1.0 or higher). If forwarding is disabled, you can use this configuration:
The PHP agent configuration docs explain in detail how to configure your agent, but here we'll review and link to some of the most important aspects of the PHP logs configuration.
There is a single option to control if the core logging feature is active:
If you're using a supported logging framework the agent can send metrics which measure the number of severity of log messages your application is generating. You can enable this features using the newrelic.application_logging.metrics.enabled config.
If you're using a supported logging framework and want to use the agent to send your application logs to New Relic, you can control that through settings newrelic.application_logging.forwarding prefixed INI settings. Options available are:
If you are using the Monolog logging library (version 2 or 3) then you may also enable log context attributes. This converts logging context data
passed to Monolog into New Relic attributes. You can control this feature through settings newrelic.application_logging.forwarding.context_data
prefixed INI settings. Options available are:
If you have an existing log forwarding solution and are updating your agent to use automatic logs in context, be sure to disable your manual log forwarder. Otherwise, your app will be sending double the log data. Depending on your account, this could result in double billing. For more information, learn how to disable your specific log forwarder.
Already have a log forwarder you like? We've got you covered! Language agents can decorate your logs with the linking metadata needed to provide access to automatic logs-in-context features.
This method requires that you install an external log forwarder before enabling logs in context. If you do not have a log forwarder, the New Relic UI will prompt you to use our infrastructure agent.
If you decide to use your existing log forwarding solution and later decide to update your agent to use automatic logs in context, be sure to disable your manual log forwarder. Otherwise, your app will be sending double log lines. Depending on your account, this could result in double billing. For more information, follow the procedures to disable your specific log forwarder.
Important
Local log decoration for the PHP agent does not directly alter log messages. Your logging framework configuration will need to be updated to write out the NR-LINKING token in messages.
If you choose to use log decoration to activate logs in context for PHP, first setup your PHP app.
Make sure you have already set up logging in New Relic. This includes configuring a supported log forwarder that collects your application logs and forwards these to New Relic.
The PHP agent will now add linking metadata to each Monolog log record. For this information to appear in the actual log messages it is necessary to set a Monolog Formatter for each Monolog Handler that includes the %extra.NR-LINKING% format specification at the end of the message. This is the necessary linking data for logs in context to work.
Our decorator adds five attributes to every log message (plain text): entity.guid, entity.name, hostname, trace.id, and span.id. Example:
This is my log message. NR-LINKING|{entity.guid}|{hostname}|{trace.id}|{span.id}|{entity.name}|
To verify that you have configured the log appender correctly, run your application, then check your logs data in New Relic using the query operator has:span.id has:trace.id.
If everything is configured correctly and your data is being forwarded to New Relic with the enriched metadata, your logs should now be emitted as JSON and contain trace.id and span.id fields. If you don't see log data in the UI, follow the troubleshooting procedures.
Did this doc help with your installation?
Secure your data
Your logs may include sensitive information protected by HIPAA or other compliance protocols. By default we obfuscate number patterns that appear to be for items such as credit cards or Social Security numbers, but you may need to hash or mask additional information.
For more information, see our documentation about obfuscation expressions and rules. You can hash or mask your log data by using the New Relic UI or by using NerdGraph, our GraphQL API.
Explore your data
To make the most of your logging data:
On the APM Summary page, click your Web transaction time chart to view logs associated with a specific point in time.
Check your app's Errors inbox to view the logs associated with your errors.
Typically your logs will start to appear less than a minute after you enable logs in context. Check your app's Triage > Logs section. You will also start seeing log patterns related to the error there.
If you don't see any logs for errors or traces, there may not be any for your app. Try refreshing the UI page, or change the selected time period.
Disable automatic logging
APM logs in context automatically forwards agent log data when enabled. As of agent release 10.3.0, this feature is enabled by default for the PHP agent. This can have a negative impact on your security, compliance, billing, or system performance. For more information, or if you need to adjust the default setting, follow the procedures to disable automatic logging.