Enable Logs-in-context with Logback

BETA

New Relic offers a Logback 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.

The code and an example application are available on GitHub.

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 Logback, 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 Logback:

Before enabling logs-in-context, you must have the appropriate feature flags. To be added, please contact rholzschuh@newrelic.com.

  1. Install or update the Java agent.
  2. Enable and configure the Logback 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 Logback extension

To enable logs-in-context with the Logback extension, complete the following steps:

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

    Update with Gradle

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

    dependencies {
      compile("com.newrelic.logging:logback: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>logback</artifactId>
            <version>1.0-rc2</version>
        </dependency>
    </dependencies>
  2. Update your logging configuration xml to replace any existing <encoder> element as shown below.

    If you are logging to the console (stdout/stderr), look for ConsoleAppender and replace :

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="com.newrelic.logging.logback.NewRelicEncoder"/>
    </appender>

    If you are logging to a file, look for FileAppender and replace <encoder>:

    <appender name="LOG_FILE" class="ch.qos.logback.core.FileAppender">
        <file>logs/app-log-file.log</file>
        <encoder class="com.newrelic.logging.logback.NewRelicEncoder"/>
    </appender>
  3. NewRelicAsyncAppender must wrap any appenders that will target New Relic's log forwarder. Update your logging configuration xml to add this section. Change "LOG_FILE" to the name of the appender you updated in the previous step.

    <appender name="ASYNC" class="com.newrelic.logging.logback.NewRelicAsyncAppender">
        <appender-ref ref="LOG_FILE" />
    </appender>
  4. NewRelicAsyncAppender should be the appender used in your logger. Replace your root logger’s appenders with the ASYNC appender created in the previous step.

    <root>
        <appender-ref ref="ASYNC" />
    </root>

Example APPENDER files for the Logback extension

You can find a working example in GitHub.

Before and after examples of an updated logging .xml file for the Logback extension.

Single appender example before configuration

Before configuration with a single appender, using XML. This only logs to the console. This may be a common configuration for containerized environments.

<configuration>
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>
  <root level="debug">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>
Single appender example after configuration

Example configuration file after adding in the logging extension information.

<configuration>
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">

    <!-- changed the encoder -->
    <encoder class="com.newrelic.logging.logback.NewRelicEncoder"/>

  </appender>

  <!-- added the ASYNC appender -->
  <appender name="ASYNC" class="com.newrelic.logging.logback.NewRelicAsyncAppender">
    <appender-ref ref="STDOUT" />
  </appender>

  <root level="debug">

    <!-- changed the root logger -->
    <appender-ref ref="ASYNC" /> 

  </root>
</configuration>
Double appender example before configuration

This example logs to both a file and to standard out.

<configuration>
  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <file>myApp.log</file>
    <encoder>
      <pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
    </encoder>
  </appender>
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%msg%n</pattern>
    </encoder>
  </appender>
  <root level="debug">
    <appender-ref ref="FILE" />
    <appender-ref ref="STDOUT" />
  </root>
</configuration>
Double appender example after configuration

Here, we've altered the file output for New Relic, and we've left standard out logging alone.

<configuration>
  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <file>myApp.log</file>
    <!-- encoder changed -->
    <encoder class="com.newrelic.logging.logback.NewRelicEncoder"/>
  </appender>
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%msg%n</pattern>
    </encoder>
  </appender>
  <appender name="ASYNC" class="com.newrelic.logging.logback.NewRelicAsyncAppender">
    <appender-ref ref="FILE" /> <!-- ASYNC appender wraps FILE -->
  </appender>
  <root level="debug">
    <appender-ref ref="ASYNC" /> <!-- FILE changed to ASYNC -->
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

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: