Java: Configure with Log4j 2.x

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

Configure logs in context with log management

To configure New Relic logs in context with Log4j 2.x:

  1. Enable logs management with a compatible log forwarding plugin.
  2. Install or update the Java agent.
  3. Configure the Log4j 2.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 2.x extension

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

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

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

    dependencies {
        compile("com.newrelic.logging:log4j2:2.0")
    }

    To update with Maven, add the highlighted section to your pom.xml file:

    <dependencies>
        <dependency>
            <groupId>com.newrelic.logging</groupId>
            <artifactId>log4j2</artifactId>
            <version>2.0</version>
        </dependency>
    </dependencies>
  2. In your logging configuration XML file, update your <configuration> element by adding the highlighted section:

    <Configuration xmlns="http://logging.apache.org/log4j/2.0/config"
            packages="com.newrelic.logging.log4j2">

    If you are using a properties file, add packages=com.newrelic.logging.log4j2.

  3. Once updated, add <NewRelicLayout/> to use a NewRelicLayout element within one of the appenders. For example:

    <File name="MyFile" fileName="logs/app-log-file.log">
        <NewRelicLayout/>
    </File>

    If you are using a properties file, change only the layout.type property, as shown below:

    appender.console.type = Console
    appender.console.name = STDOUT
    appender.console.layout.type = NewRelicLayout
    
  4. If you only modified an existing appender, you can skip this step.

    Add <AppenderRef/> within <Root> to use this appender. The ref attribute refers to the name of the appender you created in the previous step. For example:

    <Root level="info">
        <AppenderRef ref="MyFile"/>
    </Root>

    If you are using a properties file, you will need to add a property if you added a new appender:

    rootLogger.level = info
    rootLogger.appenderRef.stdout.ref = STDOUT​​​​​

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:

  • Includes trace.id and span.id fields
  • 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