Java: Configure with Log4j 1.x

New Relic offers a Log4j 1.x 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 Log4j 1.x, ensure your configuration meets the following requirements:

Configure logs in context with New Relic Logs

To configure New Relic logs in context with Logs4j 1.x:

  1. Enable New Relic Logs 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 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 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 {
    • To update with Maven, add the following to your pom.xml file:

  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"/> 
  3. Once updated, use NewRelicAsyncAppender to wrap any appenders that will target New Relic's log forwarder. For example:

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

        <appender-ref ref="NewRelicFile" />

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"/>

    <!-- this appender was added -->
    <appender name="NewRelicFile" 
        <appender-ref ref="TypicalFile" />

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

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


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 and 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:

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: