New Relic offers a Logback 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.
To use New Relic logs in context with Logback, ensure your configuration meets the following requirements:
- Java agent 5.6.0 or higher: Install or update
- Logback 1.2.0 or higher installed and working on the application.
To configure New Relic logs in context with Logback:
- Enable log management with a compatible log forwarding plugin.
- Install or update the Java agent.
- Configure the Logback extension.
- Check for logging data.
To configure logs in context with the Logback extension, complete the following steps:
Update your project's dependencies to include the Logback extension as applicable:
To update with Gradle, add the following to your
To update with Maven, add the following to your
Update your logging configuration xml to replace any existing <encoder> element as shown below.
If you are logging to the console (stdout/stderr), look for
ConsoleAppenderand replace :<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"><encoder class="com.newrelic.logging.logback.NewRelicEncoder"/></appender>
If you are logging to a file, look for
<encoder>:<appender name="LOG_FILE" class="ch.qos.logback.core.FileAppender"><file>logs/app-log-file.log</file><encoder class="com.newrelic.logging.logback.NewRelicEncoder"/></appender>
Update your logging configuration xml with the
NewRelicAsyncAppender. To ensure that
NewRelicAsyncAppenderwraps any appenders that will target New Relic's log forwarder, add the following section. Change
"LOG_FILE"to the name of the appender you updated in the previous step.<appender name="ASYNC" class="com.newrelic.logging.logback.NewRelicAsyncAppender"><appender-ref ref="LOG_FILE" /></appender>
NewRelicAsyncAppenderis the appender used in your logger. Replace your root logger’s appenders with the
ASYNCappender created in the previous step.<root><appender-ref ref="ASYNC" /></root>
It's important that the
NewRelicAsyncAppenderbe the first appender to see the log message. List any other appenders after the
You can find a working example in GitHub.
Here are examples of an updated logging .xml file for the Logback extension.
To verify that you have configured the extension correctly, run your application and verify that the logging you have configured contains the following:
- Is properly-formatted JSON lines
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.
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.