Enable Logs-in-context with Log4j 1.x

BETA

New Relic offers a Log4j 1.x 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.

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 Log4j 1.x, 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 Logs4j 1.x:

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 Log4j 1.x 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 Log4j 1.x extension

To enable 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:

    Update with Gradle

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

    dependencies {
      compile("com.newrelic.logging:log4j1: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>log4j1</artifactId>
        <version>1.0-rc2</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>

Example APPENDER files for the Log4j 1.x extension

Before and after examples of an updated logging .xml file for the Log4j 1.x extension.

Example .xml before configuration

Before, using XML. Note that we are only going to change the FILE appender, not the CONSOLE appender. But the process is nearly identical if you are changing the CONSOLE appender.

<?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 class="org.apache.log4j.PatternLayout"> 
           <param name="ConversionPattern" value="%-5p %c{1} - %m%n"/> 
        </layout> 
    </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>
        <appender-ref ref="TypicalFile" />
        <appender-ref ref="TypicalConsole" />
    </root>
</log4j:configuration>
Example .xml after configuration

Example configuration file after adding in the logging extension information.

<?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 class="com.newrelic.logging.log4j1.NewRelicLayout"/>
    </appender>

    
    <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>        
        <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 enabled logs-in-context, here are some potential next steps:

For more help

Recommendations for learning more: