Enable Logs-in-context with java-util-logging

BETA

New Relic offers a java.util.logging extension to connect your log data to New Relic Logs-in-context for Java, allowing you link to your log data with related data across the rest of the New Relic platform.​ This document explains how to enable this feature and get started using it.

New Relic's logs-in-context (linking logs directly to Applications, APM errors, and APM traces and spans) for New Relic Logs is currently available as a beta feature. Your use of the early access service is at your own risk. New Relic disclaims all warranties, express or implied, regarding the beta services.

Compatibility and requirements

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

New Relic Logs must be enabled and a compatible log forwarder must be installed prior to enabling logs-in-context. For more information, see Introduction to New Relic Logs.

Enable logs-in-context with New Relic Logs

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

  1. Install or update the Java agent.
  2. Enable and configure the java-util-logging extension.
  3. Check for logging data.

Install or update the Java agent

Install or update to the most recent Java agent version, and enable Distributed tracing.

Enable and configure the java-util-logging extension

To enable 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:

    Update with Gradle

    To update with Gradle, add the following to your build.gradle file:

    dependencies {
        compile("com.newrelic.logging:jul:1.0-rc2")
    }
    Update with Maven

    To update with Maven, add the following to your pom.xml file:

    <dependencies>
        <dependency>
            <groupId>com.newrelic.logging</groupId>
            <artifactId>jul</artifactId>
            <version>1.0-rc2</version>
        </dependency>
    </dependencies>
  2. In your logging properties file, locate the root logger's handlers. Example:

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

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

    Note: Once complete, JSON will be 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. Configure the NewRelicMemoryHandler by setting the target to the handler as shown:

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

    Note: Once complete, the JSON should be updated to include fields like entity.name. 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:

  1. Includes trace.id and span.id fields
  2. 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 enabled logs-in-context, here are some potential next steps:

For more help

Recommendations for learning more: