• /
  • Log in
  • Free account

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:

  • Java agent 5.6.0 or higher: Install or update
  • JVM argument -javaagent enabled on the Java agent.
  • Log4j 1.x package installed and working on the application.
  • log4j must be configured in code or via XML. Properties files are not supported because AsyncAppender instances can only be automatically configured via XML.

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 {
    • 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"/> <!-- only this line needs to be added -->
  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 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:

Create issueEdit page
Copyright © 2021 New Relic Inc.