Enable Logs-in-context with Log4j 2.x

BETA

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

  1. Install or update the Java agent.
  2. Enable and configure the Log4j 2.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 2.x extension

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

    Update with Gradle

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

    dependencies {
        compile("com.newrelic.logging:log4j2:1.0-rc2")
    }
    Update with Maven

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

    <dependencies>
        <dependency>
            <groupId>com.newrelic.logging</groupId>
            <artifactId>log4j2</artifactId>
            <version>1.0-rc2</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">

    Note: If 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 use 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 use a properties file, you will need to add a property if you added a new appender:

    rootLogger.level = info
    rootLogger.appenderRef.stdout.ref = STDOUT
    
  5. Set the log4j2.messageFactory system property to the NewRelicMessageFactory. The exact method for making this change many vary, depending on your application framework. One of options listed below may work for you, or you may refer to the log4j 2.x documentation for more information on the MessageFactory:

    • One option is to change the java command-line with -D. Many frameworks use the JAVA_OPTS environment variable.
    • If you have application properties files in your project, you can try adding the following:
      log4j2.messageFactory=com.newrelic.logging.log4j2.NewRelicMessageFactory

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: