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.
Compatibility and requirements
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
-javaagent
enabled on the Java agent. - java.util.logging package configured and working on the application.
Configure logs in context with log management
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.
Enable log management
Confirm that you have log management enabled, with a compatible log forwarding plugin installed to send your application logs to New Relic.
Install or update the Java agent
Install or update to the most recent Java agent version, and enable Distributed tracing.
Configure the java.util.logging extension
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
build.gradle
file:dependencies {compile("com.newrelic.logging:jul:2.0")}To update with Maven, add the following to your
pom.xml
file:<dependencies><dependency><groupId>com.newrelic.logging</groupId><artifactId>jul</artifactId><version>2.0</version></dependency></dependencies>Tip
Can't edit these files? You can add the jars directly to the application classpath.
Use the
NewRelicMemoryHandler
to 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.FileHandlerUpdate your logging properties file set the root logger's handler.
handlers = com.newrelic.logging.jul.NewRelicMemoryHandlerConfigure the
NewRelicMemoryHandler
by 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.FileHandlerTip
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
MemoryHandler
implementation intercepts that data.Use a
NewRelicFormatter
for the final handler.Update your logging properties file to set the
formatter
property like the example below. Note: the handler on which you set the formatter must be thetarget
handler from the previous step (java.util.logging.FileHandler
in this example).java.util.logging.FileHandler.formatter = com.newrelic.logging.jul.NewRelicFormatterTip
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.
Tip
Once complete, JSON is logged instead of text. The JSON should be formatted as single objects, one per line, and should contain fields like
log.level
andthread.name
. Thetrace.id
, which is required for Logs in Context,should only have a value for log messages that occur within a transaction.
Check for logging data
To verify that you have configured the extension correctly, run your application and verify that the logging you have configured contains the following:
- Includes
trace.id
andspan.id
fields - Is properly-formatted JSON lines
If everything is configured correctly and your data is being reported, you should see data logs in the New Relic Logs UI using the query operator has: span.id/trace.id
.
What's next?
Now that you've set up APM logs in context, here are some potential next steps:
- Explore your data using the Logs UI.
- Troubleshoot errors with distributed tracing, stack traces, application logs, and more.
- Query your data and create custom dashboards or alerts.
For more help
If you need more help, check out these support and learning resources:
- Browse the Explorers Hub to get help from the community and join in discussions.
- Find answers on our sites and learn how to use our support portal.
- Run New Relic Diagnostics, our troubleshooting tool for Linux, Windows, and macOS.
- Review New Relic's data security and licenses documentation.