Log4j 2 Integration

Learn how to use Sentry's Log4j 2 integration with Spring Boot.

For the best experience, we recommend using Sentry's Spring Boot integration with the Log4j 2 logging framework integration as they work together seamlessly.

To use Sentry's Log4j 2 integration in Spring Boot application, you must include a dependency to the sentry-log4j2 module:

Copied
<dependency>
    <groupId>io.sentry</groupId>
    <artifactId>sentry-log4j2</artifactId>
    <version>8.30.0</version>
</dependency>

For other dependency managers see the central Maven repository.

To send logs to Sentry and have them show up in the Logs section, you need to enable the feature:

Copied
sentry.logs.enabled=true

Follow the guide on configuring Log4j 2 with Spring Boot and configure SentryAppender in the log4j2.xml file:

Copied
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration>

  <Appenders>
    <Console name="CONSOLE" target="SYSTEM_OUT">
      <PatternLayout
        pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"
      />
    </Console>
    <Sentry name="SENTRY" minimumLevel="DEBUG" />
  </Appenders>

  <Loggers>
    <Root level="INFO">
      <AppenderRef ref="CONSOLE" />
      <AppenderRef ref="SENTRY" />
    </Root>
  </Loggers>

</Configuration>

Starting with Sentry Java SDK version 8.24.0, you can use the contextTags option to include specific properties from the Mapped Diagnostic Context (MDC) as attributes on log entries sent to Sentry.

Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").