Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<scope>import</scope>
</dependency>
```
- Add log4j2 spring boot 4 autoconfiguration ([#5403](https://github.com/getsentry/sentry-java/pull/5403))

### Fixes

Expand Down
3 changes: 3 additions & 0 deletions sentry-log4j2/api/sentry-log4j2.api
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public class io/sentry/log4j2/SentryAppender : org/apache/logging/log4j/core/app
public static fun createAppender (Ljava/lang/String;Lorg/apache/logging/log4j/Level;Lorg/apache/logging/log4j/Level;Lorg/apache/logging/log4j/Level;Ljava/lang/String;Ljava/lang/Boolean;Lorg/apache/logging/log4j/core/Filter;Ljava/lang/String;)Lio/sentry/log4j2/SentryAppender;
protected fun createBreadcrumb (Lorg/apache/logging/log4j/core/LogEvent;)Lio/sentry/Breadcrumb;
protected fun createEvent (Lorg/apache/logging/log4j/core/LogEvent;)Lio/sentry/SentryEvent;
public fun getMinimumBreadcrumbLevel ()Lorg/apache/logging/log4j/Level;
public fun getMinimumEventLevel ()Lorg/apache/logging/log4j/Level;
public fun getMinimumLevel ()Lorg/apache/logging/log4j/Level;
public fun start ()V
}

Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,18 @@ public void start() {
start(getOptionsConfiguration(null));
}

public @NotNull Level getMinimumBreadcrumbLevel() {
return minimumBreadcrumbLevel;
}

public @NotNull Level getMinimumEventLevel() {
return minimumEventLevel;
}

public @NotNull Level getMinimumLevel() {
return minimumLevel;
}

@NotNull
Sentry.OptionsConfiguration<SentryOptions> getOptionsConfiguration(
final @Nullable Sentry.OptionsConfiguration<SentryOptions> additionalOptionsConfiguration) {
Expand Down
13 changes: 13 additions & 0 deletions sentry-samples/sentry-samples-spring-boot-4/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ dependencies {
implementation(projects.sentryQuartz)
implementation(projects.sentryAsyncProfiler)

implementation(projects.sentryLog4j2)
implementation(libs.log4j.api)
implementation(libs.log4j.core)

// Enable Log4j2 in Spring Boot
// implementation("org.springframework.boot:spring-boot-starter-log4j2")
// modules {
// module("org.springframework.boot:spring-boot-starter-logging") {
// replacedBy("org.springframework.boot:spring-boot-starter-log4j2", "Use Log4j2 instead of
// Logback")
// }
// }

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incomplete Log4j2 sample setup

Low Severity

The sample adds sentry-log4j2 plus log4j-core while keeping Logback, and leaves the Spring Boot Log4j2 replacement block commented out. That looks like unfinished WIP and can put conflicting Log4j2 providers on the classpath without actually demonstrating the new auto-configuration.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 2493682. Configure here.


// cache tracing
implementation(libs.springboot4.starter.cache)
implementation(libs.caffeine)
Expand Down
11 changes: 11 additions & 0 deletions sentry-spring-boot-4/api/sentry-spring-boot-4.api
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ public class io/sentry/spring/boot4/SentryAutoConfiguration {
public fun <init> ()V
}

public class io/sentry/spring/boot4/SentryLog4j2AppenderAutoConfiguration {
public fun <init> ()V
public fun sentryLog4j2Initializer (Lio/sentry/spring/boot4/SentryProperties;)Lio/sentry/spring/boot4/SentryLog4j2Initializer;
}

public class io/sentry/spring/boot4/SentryLog4j2Initializer : org/springframework/context/event/GenericApplicationListener {
public fun <init> (Lio/sentry/spring/boot4/SentryProperties;)V
public fun onApplicationEvent (Lorg/springframework/context/ApplicationEvent;)V
public fun supportsEventType (Lorg/springframework/core/ResolvableType;)Z
}

public class io/sentry/spring/boot4/SentryLogbackAppenderAutoConfiguration {
public fun <init> ()V
public fun sentryLogbackInitializer (Lio/sentry/spring/boot4/SentryProperties;)Lio/sentry/spring/boot4/SentryLogbackInitializer;
Expand Down
6 changes: 6 additions & 0 deletions sentry-spring-boot-4/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ dependencies {
api(projects.sentry)
api(projects.sentrySpring7)
compileOnly(projects.sentryLogback)
compileOnly(projects.sentryLog4j2)
compileOnly(projects.sentryApacheHttpClient5)
compileOnly(libs.log4j.api)
compileOnly(libs.log4j.core)
compileOnly(platform(SpringBootPlugin.BOM_COORDINATES))
compileOnly(projects.sentryGraphql)
compileOnly(projects.sentryGraphql22)
Expand Down Expand Up @@ -65,7 +68,10 @@ dependencies {

// tests
testImplementation(projects.sentryLogback)
testImplementation(projects.sentryLog4j2)
testImplementation(projects.sentryApacheHttpClient5)
testImplementation(libs.log4j.api)
testImplementation(libs.log4j.core)
testImplementation(projects.sentryGraphql)
testImplementation(projects.sentryGraphql22)
testImplementation(projects.sentryKafka)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.sentry.spring.boot4;

import com.jakewharton.nopen.annotation.Open;
import io.sentry.log4j2.SentryAppender;
import org.apache.logging.log4j.core.LoggerContext;
import org.jetbrains.annotations.NotNull;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/** Auto-configures {@link SentryAppender}. */
@Configuration(proxyBeanMethods = false)
@Open
@ConditionalOnClass({LoggerContext.class, SentryAppender.class})
@ConditionalOnProperty(name = "sentry.logging.enabled", havingValue = "true", matchIfMissing = true)
@ConditionalOnBean(SentryProperties.class)
public class SentryLog4j2AppenderAutoConfiguration {

@Bean
public @NotNull SentryLog4j2Initializer sentryLog4j2Initializer(
final @NotNull SentryProperties sentryProperties) {
return new SentryLog4j2Initializer(sentryProperties);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package io.sentry.spring.boot4;

import com.jakewharton.nopen.annotation.Open;
import io.sentry.ScopesAdapter;
import io.sentry.log4j2.SentryAppender;
import io.sentry.util.Objects;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.LoggerConfig;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.GenericApplicationListener;
import org.springframework.core.ResolvableType;

/** Registers {@link SentryAppender} after Spring context gets refreshed. */
@Open
public class SentryLog4j2Initializer implements GenericApplicationListener {
private static final Logger logger = LoggerFactory.getLogger(SentryLog4j2Initializer.class);
private static final String SENTRY_APPENDER_NAME = "SENTRY_APPENDER";

private final @NotNull SentryProperties sentryProperties;
private final @NotNull List<String> loggers;
@Nullable private SentryAppender sentryAppender;

public SentryLog4j2Initializer(final @NotNull SentryProperties sentryProperties) {
this.sentryProperties = Objects.requireNonNull(sentryProperties, "properties are required");
loggers = sentryProperties.getLogging().getLoggers();
}

@Override
public boolean supportsEventType(final @NotNull ResolvableType eventType) {
return eventType.getRawClass() != null
&& ContextRefreshedEvent.class.isAssignableFrom(eventType.getRawClass());
}

@Override
public void onApplicationEvent(final @NotNull ApplicationEvent event) {
final Object context = LogManager.getContext(false);
if (!(context instanceof LoggerContext)) {
logger.info(
"Sentry Log4j2 appender was not configured because Log4j2 Core is not the active logging backend. Log4j2 API calls may be routed through SLF4J.");
return;
}

final LoggerContext loggerContext = (LoggerContext) context;
final Configuration configuration = loggerContext.getConfiguration();

boolean changed = false;
for (final String loggerName : normalizeLoggerNames(loggers)) {
final LoggerConfig loggerConfig = getOrCreateLoggerConfig(configuration, loggerName);
if (!isSentryAppenderRegistered(loggerConfig)) {
loggerConfig.addAppender(getSentryAppender(configuration), null, null);
changed = true;
}
}

if (changed) {
loggerContext.updateLoggers(configuration);
}
}

private @NotNull LoggerConfig getOrCreateLoggerConfig(
final @NotNull Configuration configuration, final @NotNull String loggerName) {
if (LogManager.ROOT_LOGGER_NAME.equals(loggerName)) {
return configuration.getRootLogger();
}

final LoggerConfig loggerConfig = configuration.getLoggerConfig(loggerName);
if (loggerName.equals(loggerConfig.getName())) {
return loggerConfig;
}

final LoggerConfig newLoggerConfig = new LoggerConfig(loggerName, null, true);
newLoggerConfig.setParent(loggerConfig);
configuration.addLogger(loggerName, newLoggerConfig);
return newLoggerConfig;
}

private @NotNull SentryAppender getSentryAppender(final @NotNull Configuration configuration) {
if (sentryAppender == null) {
sentryAppender =
new SentryAppender(
SENTRY_APPENDER_NAME,
null,
null,
toLog4jLevel(sentryProperties.getLogging().getMinimumBreadcrumbLevel()),
toLog4jLevel(sentryProperties.getLogging().getMinimumEventLevel()),
toLog4jLevel(sentryProperties.getLogging().getMinimumLevel()),
null,
null,
ScopesAdapter.getInstance(),
null);
sentryAppender.start();
configuration.addAppender(sentryAppender);
}
return sentryAppender;
}

private @NotNull Set<String> normalizeLoggerNames(final @NotNull List<String> loggerNames) {
final Set<String> normalized = new LinkedHashSet<>();
for (final String loggerName : loggerNames) {
if (loggerName == null || loggerName.trim().isEmpty()) {
continue;
}
normalized.add(normalizeLoggerName(loggerName.trim()));
}
return normalized;
}

private boolean isSentryAppenderRegistered(final @NotNull LoggerConfig loggerConfig) {
return loggerConfig.getAppenders().values().stream()
.anyMatch(appender -> appender.getClass().equals(SentryAppender.class));
}

private @NotNull String normalizeLoggerName(final @NotNull String loggerName) {
if (org.slf4j.Logger.ROOT_LOGGER_NAME.equals(loggerName)) {
return LogManager.ROOT_LOGGER_NAME;
}
return loggerName;
}

private @Nullable Level toLog4jLevel(final @Nullable org.slf4j.event.Level slf4jLevel) {
return slf4jLevel == null ? null : Level.getLevel(slf4jLevel.name());
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
io.sentry.spring.boot4.SentryAutoConfiguration
io.sentry.spring.boot4.SentryProfilerAutoConfiguration
io.sentry.spring.boot4.SentryLogbackAppenderAutoConfiguration
io.sentry.spring.boot4.SentryLog4j2AppenderAutoConfiguration
io.sentry.spring.boot4.SentryWebfluxAutoConfiguration
Loading
Loading