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:
- Enable log management with a compatible log forwarding plugin.
- Install or update the Java agent.
- Configure the Log4j 1.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 1.x extension
To configure logs in context with the Log4j 1.x extension, complete the following steps:
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 {compile("com.newrelic.logging:log4j1:2.0")}To update with Maven, add the following to your
pom.xml
file:<dependencies><dependency><groupId>com.newrelic.logging</groupId><artifactId>log4j1</artifactId><version>2.0</version></dependency></dependencies>
In your logging configuration XML file, update your
<appender>
element with aNewRelicLayout
, 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 --> </appender>
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>
Once updated, use the async appender on the root logger. For example:
<root> <appender-ref ref="NewRelicFile" /> </root>
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"/>
</appender>
<!-- this appender was added -->
<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>
<!-- the new appender was used here -->
<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:
- 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.