Java: Configure with Dropwizard

New Relic offers a Dropwizard extension for log management, allowing you link to your log data with related data across the rest of the New Relic platform. Read on to learn 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 Dropwizard 1.3, 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.
  • Dropwizard 1.3 package installed and working on the application, with the original Dropwizard appenders and logging factory.

Configure logs in context with New Relic Logs

To configure logs in context with Dropwizard:

  1. Enable log monitoring with a compatible log forwarding plugin.
  2. Install or update the Java agent.
  3. Configure the Dropwizard extension.
  4. Check for logging data.

Enable log monitoring

Confirm that you have log monitoring 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 Dropwizard extension

To configure logs in context with the Dropwizard 1.3 extension, complete the following steps:

  1. Update your project's dependencies to include the Dropwizard 1.3 extension as applicable:

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

      dependencies {
          compile("com.newrelic.logging:dropwizard:2.0")
      }
    • To update with Maven, add the following to your pom.xml file:

      <dependencies>
          <dependency>
              <groupId>com.newrelic.logging</groupId>
              <artifactId>dropwizard</artifactId>
              <version>2.0</version>
          </dependency>
      </dependencies>
  2. Update your Dropwizard .yaml configuration file with a newrelic-json layout, replacing the currently used type: console or type: file with either type: newrelic-console or type: newrelic-file as appropriate. For example:
    logging:
      appenders:
        - type: newrelic-console
          # Add the two lines below if you don't have a layout specified on the appender.
          # If you have a layout, remove all parameters to the layout and set the type.
          layout: 
            type: newrelic-json

    Alternatively, the New Relic Dropwizard extension also supports a log-format layout type that uses the standard Dropwizard logging. For testing purposes, you can change the type of the layout with a one-line change

    logging:
      appenders:
        - type: newrelic-file
          # This format will be ignored by the newrelic-json layout, but used by the log-format layout.
          logFormat: "%date{ISO8601} %c %-5p: %m trace.id=%mdc{trace.id} span.id=%mdc{span.id}%n"
          layout: 
            # type: newrelic-json
            type: log-format

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