New Relic offers a java.util.logging extension for log management, allowing you link to your log data with related data across the rest of the New Relic platform. This document explains how to configure logs in context and start getting log data. The code and an example application are available on GitHub.
To use New Relic logs in context with java.util.logging, ensure your configuration meets the following requirements:
- Java agent 5.6.0 or higher: Install or update
- JVM argument
-javaagentenabled on the Java agent.
- java.util.logging package configured and working on the application.
To configure New Relic logs in context with java-util-logging:
- Enable log management with a compatible log forwarding plugin.
- Install or update the Java agent.
- Configure the java.util.logging extension.
- Check for logging data.
To configure logs in context with the java.util.logging extension, complete the following steps:
Update your project's dependencies to include the java.util.logging extension as applicable:
To update with Gradle, add the following to your
To update with Maven, add the following to your
Can't edit these files? You can add the jars directly to the application classpath.
NewRelicMemoryHandlerto intercept messages destined for another handler. Your file will have a handlers property, but it might be set to something else. Your logging properties file will include a line listing the root logger's handlers, like this:handlers = java.util.logging.FileHandler
Update your logging properties file set the root logger's handler.handlers = com.newrelic.logging.jul.NewRelicMemoryHandler
NewRelicMemoryHandlerby setting the target to the handler that was previously set for the handlers property. The value of this property should be the handler that was previously assigned to the root logger.com.newrelic.logging.jul.NewRelicMemoryHandler.target = java.util.logging.FileHandler
Why? The New Relic log format includes data from the New Relic agent that must be captured on the thread the log message is coming from. This
MemoryHandlerimplementation intercepts that data.
NewRelicFormatterfor the final handler.
Update your logging properties file to set the
formatterproperty like the example below. Note: the handler on which you set the formatter must be the
targethandler from the previous step (
java.util.logging.FileHandlerin this example).java.util.logging.FileHandler.formatter = com.newrelic.logging.jul.NewRelicFormatter
Why? The New Relic log format is a tailored JSON format with specific fields in specific places that our log forwarder plugins and back end rely on. At this time, we don't support any customization of that format.
Once complete, JSON is logged instead of text. The JSON should be formatted as single objects, one per line, and should contain fields like
trace.id, which is required for Logs in Context,should only have a value for log messages that occur within a transaction.
To verify that you have configured the extension correctly, run your application and verify that the logging you have configured contains the following:
- Is properly-formatted JSON lines
Now that you've set up APM logs in context, here are some potential next steps: