Java: Configure with Log4j 1.x

New Relic offers a Log4j 1.x extension for New Relic log management, 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

To use New Relic logs in context with Log4j 1.x, ensure your configuration meets the following requirements:

Configure logs in context with log management

To configure logs in context with Logs4j 1.x:

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

Enable log management

Confirm that you have log management 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 Log4j 1.x extension

To configure logs in context with the Log4j 1.x extension, complete the following steps:

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

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

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

      <dependencies>
        <dependency>
          <groupId>com.newrelic.logging</groupId>
          <artifactId>log4j1</artifactId>
          <version>2.0</version>
        </dependency>
      </dependencies>
  2. In your logging configuration XML file, update your <appender> element with a NewRelicLayout, adding <layout class="com.newrelic.logging.log4j1.NewRelicLayout"/> as shown below:

    <appender name="TypicalFile" class="org.apache.log4j.FileAppender">
      <param name="file" value="logs/log4j1-app.log"/>
      <param name="append" value="false"/>
      <layout class="com.newrelic.logging.log4j1.NewRelicLayout"/> 
    </appender>
  3. Once updated, use NewRelicAsyncAppender to wrap any appenders that will target New Relic's log forwarder. For example:

    <appender name="NewRelicFile" 
              class="com.newrelic.logging.log4j1.NewRelicAsyncAppender">
      <appender-ref ref="TypicalFile" />
    </appender>
  4. Once updated, use the async appender on the root logger. For example:

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

Example configuration file for the Log4j 1.x extension

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="false">
    <appender name="TypicalFile" class="org.apache.log4j.FileAppender">
        <param name="file" value="logs/log4j1-app.log"/>
        <param name="append" value="false"/>
        <!-- layout has been replaced -->
        <layout class="com.newrelic.logging.log4j1.NewRelicLayout"/>
    </appender>

    <!-- this appender was added -->
    <appender name="NewRelicFile" 
              class="com.newrelic.logging.log4j1.NewRelicAsyncAppender">
        <appender-ref ref="TypicalFile" />
    </appender>

    <appender name="TypicalConsole" class="org.apache.log4j.ConsoleAppender">
      <layout class="org.apache.log4j.PatternLayout"> 
        <param name="ConversionPattern" value="%-5p %c{1} - %m%n"/> 
       </layout> 
    </appender>

    <root>        ​
        <!-- the new appender was used here -->​​
        <appender-ref ref="NewRelicFile" />
        <appender-ref ref="TypicalConsole" />
    </root>

</log4j: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 set up APM logs in context, here are some potential next steps:

For more help

If you need more help, check out these support and learning resources: