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:
- Java agent 5.6.0 or higher: Install or update
- JVM argument
-javaagent
enabled on the Java agent. - Log4j 2.x or Logs4j 2 binding package installed and working on the application.
Configure logs in context with log management
To configure New Relic logs in context with Log4j 2.x:
- Enable logs management with a compatible log forwarding plugin.
- Install or update the Java agent.
- Configure the Log4j 2.x extension.
- 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:
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>
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
.Once updated, add
<NewRelicLayout/>
to use aNewRelicLayout
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
If you only modified an existing appender, you can skip this step.
Add
<AppenderRef/>
within<Root>
to use this appender. Theref
attribute refers to thename
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
andspan.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:
- Explore your data using the Logs UI.
- Troubleshoot errors with distributed tracing, stack traces, application logs, and more.
- Query your data and create custom dashboards or alerts.
For more help
If you need more help, check out these support and learning resources:
- Browse the Explorers Hub to get help from the community and join in discussions.
- Find answers on our sites and learn how to use our support portal.
- Run New Relic Diagnostics, our troubleshooting tool for Linux, Windows, and macOS.
- Review New Relic's data security and licenses documentation.