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>
  2. In your logging properties file, locate the root logger's handlers. Example:

    handlers = java.util.logging.FileHandler
  3. In your logging properties file, set the formatter property:

    java.util.logging.FileHandler.formatter = com.newrelic.logging.jul.NewRelicFormatter

    Once complete, JSON is logged instead of text. The JSON should be formatted as single objects, one per line, and should contain fields like thread.name and log.level.

  4. In your logging properties file, configure the NewRelicMemoryHandler by setting the target to the handler as shown:

    com.newrelic.logging.jul.NewRelicMemoryHandler.target = java.util.logging.FileHandler

    Once complete, the JSON is updated to include fields like entity.name. The trace.id 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: