Java: Configure with java.util.logging

New Relic offers a java.util.logging extension for New Relic Logs, 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

Access to logs in context requires a New Relic APM Pro or Pro trial subscription and enabling of distributed tracing. Before enabling distributed tracing, read the transition guide to learn about the effects on existing APM features and set-up recommendations.

To use New Relic logs in context with java.util.logging, ensure your configuration meets the following requirements:

Configure logs in context with New Relic Logs

To configure New Relic logs in context with java-util-logging:

  1. Enable New Relic Logs with a compatible log forwarding plugin.
  2. Install or update the Java agent.
  3. Configure the java.util.logging extension.
  4. Check for logging data.

Enable New Relic Logs

Confirm that you have New Relic Logs 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:

  1. 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:1.1")
      }
    • To update with Maven, add the following to your pom.xml file:

      <dependencies>
          <dependency>
              <groupId>com.newrelic.logging</groupId>
              <artifactId>jul</artifactId>
              <version>1.1</version>
          </dependency>
      </dependencies>

      Can't edit these files? You can add the jars directly to the application classpath.

  2. 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.FileHandler 
    
  3. Update your logging properties file set the root logger's handler.

    handlers = com.newrelic.logging.jul.NewRelicMemoryHandler
    
  4. Configure 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.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 MemoryHandler implementation intercepts that data.

  5. 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 the target handler from the previous step (java.util.logging.FileHandler in 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 likelog.level and thread.name. The trace.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 and span.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:

For more help

Recommendations for learning more: