Log4j 2 Integration for Spring Boot
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.
Logs over Breadcrumbs
By default this integration captures logs as breadcrumbs and error events (great for error context!). But if you need to search and query your logs across your entire application, we recommend enabling the new logs feature. Logs at or above the minimumLevel are automatically sent as Sentry Logs when enabled.
Spring Boot uses Logback by default, so first follow the guide on configuring Log4j 2 with Spring Boot to make Log4j 2 the active logging backend.
Then, to use Sentry's Log4j 2 integration in your Spring Boot application, you must include a dependency to the sentry-log4j2 module:
<dependency>
<groupId>io.sentry</groupId>
<artifactId>sentry-log4j2</artifactId>
<version>8.63.0</version>
</dependency>
<dependency>
<groupId>io.sentry</groupId>
<artifactId>sentry-log4j2</artifactId>
<version>8.63.0</version>
</dependency>
implementation 'io.sentry:sentry-log4j2:8.63.0'
libraryDependencies += "io.sentry" % "sentry-log4j2" % "8.63.0"
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:
sentry.logs.enabled=true
sentry.logs.enabled=true
For Spring Boot 3 and 4, Sentry can register SentryAppender for you once the application context refreshes. This auto-configuration is opt-in:
sentry.logging.enabled=true
sentry.logging.enabled=true
If Log4j 2 isn't the active logging backend, the appender isn't added.
Minimum logging levels for SentryAppender can be configured in application.properties or application.yml file.
sentry.logging.minimum-event-level=info
sentry.logging.minimum-breadcrumb-level=debug
sentry.logging.minimum-level=debug
sentry.logging.minimum-event-level=info
sentry.logging.minimum-breadcrumb-level=debug
sentry.logging.minimum-level=debug
The default values are:
infoor higher to include a log message as breadcrumb.infoor higher will send a log message to Sentry and will show up in the Logs section.erroror higher will send an event to Sentry and will show up in the Issues section.
The appender is added to the root logger by default. To attach it to specific loggers instead, configure their names:
sentry.logging.loggers[0]=com.example.orders
sentry.logging.loggers[1]=com.example.payments
sentry.logging.loggers[0]=com.example.orders
sentry.logging.loggers[1]=com.example.payments
Use non-overlapping logger names. Log4j 2 loggers are additive by default, so attaching the appender to both a logger and one of its ancestors can send the same log entry twice.
Spring Boot 2 doesn't support SentryAppender auto-configuration, and errors logged before the Spring context refreshes aren't captured by it. In both cases, leave sentry.logging.enabled unset and configure SentryAppender in the log4j2.xml file:
<?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>
<?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>
You do not need to configure your DSN in the Log4j 2 configuration file since Sentry is configured from the Spring Boot integration.
However, if errors that may appear during startup should to be sent to Sentry, the DSN must be provided to both the Log4j 2 and Spring Boot configurations.
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.
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").