Skip to content

Commit d896d43

Browse files
committed
remove ancestor safeguard to match logback, as the user defines which loggers to attach to
1 parent a3d7b83 commit d896d43

3 files changed

Lines changed: 18 additions & 86 deletions

File tree

sentry-samples/sentry-samples-spring-boot-4/build.gradle.kts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,19 @@ dependencies {
5757
implementation(projects.sentryQuartz)
5858
implementation(projects.sentryAsyncProfiler)
5959

60+
implementation(projects.sentryLog4j2)
61+
implementation(libs.log4j.api)
62+
implementation(libs.log4j.core)
63+
64+
// Enable Log4j2 in Spring Boot
65+
// implementation("org.springframework.boot:spring-boot-starter-log4j2")
66+
// modules {
67+
// module("org.springframework.boot:spring-boot-starter-logging") {
68+
// replacedBy("org.springframework.boot:spring-boot-starter-log4j2", "Use Log4j2 instead of
69+
// Logback")
70+
// }
71+
// }
72+
6073
// cache tracing
6174
implementation(libs.springboot4.starter.cache)
6275
implementation(libs.caffeine)

sentry-spring-boot-4/src/main/java/io/sentry/spring/boot4/SentryLog4j2Initializer.java

Lines changed: 5 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,11 @@ public void onApplicationEvent(final @NotNull ApplicationEvent event) {
5454
final LoggerContext loggerContext = (LoggerContext) context;
5555
final Configuration configuration = loggerContext.getConfiguration();
5656

57-
final Set<String> loggerNames = normalizeLoggerNames(loggers);
5857
boolean changed = false;
59-
for (final String loggerName : loggerNames) {
60-
if (isCoveredByAncestorLogger(configuration, loggerName, loggerNames)
61-
|| hasSentryAppenderRegisteredOnAncestor(configuration, loggerName)) {
62-
continue;
63-
}
64-
58+
for (final String loggerName : normalizeLoggerNames(loggers)) {
6559
final LoggerConfig loggerConfig = getOrCreateLoggerConfig(configuration, loggerName);
6660
if (!isSentryAppenderRegistered(loggerConfig)) {
67-
final SentryAppender sentryAppender = getSentryAppender(configuration);
68-
loggerConfig.addAppender(sentryAppender, null, null);
61+
loggerConfig.addAppender(getSentryAppender(configuration), null, null);
6962
changed = true;
7063
}
7164
}
@@ -113,78 +106,14 @@ public void onApplicationEvent(final @NotNull ApplicationEvent event) {
113106
}
114107

115108
private @NotNull Set<String> normalizeLoggerNames(final @NotNull List<String> loggerNames) {
116-
final Set<String> normalizedLoggerNames = new LinkedHashSet<>();
109+
final Set<String> normalized = new LinkedHashSet<>();
117110
for (final String loggerName : loggerNames) {
118111
if (loggerName == null || loggerName.trim().isEmpty()) {
119112
continue;
120113
}
121-
normalizedLoggerNames.add(normalizeLoggerName(loggerName.trim()));
122-
}
123-
return normalizedLoggerNames;
124-
}
125-
126-
private boolean isCoveredByAncestorLogger(
127-
final @NotNull Configuration configuration,
128-
final @NotNull String loggerName,
129-
final @NotNull Set<String> loggerNames) {
130-
return loggerNames.stream()
131-
.anyMatch(
132-
candidate ->
133-
isAncestorLogger(candidate, loggerName)
134-
&& isAdditivePathToAncestor(configuration, loggerName, candidate));
135-
}
136-
137-
private boolean hasSentryAppenderRegisteredOnAncestor(
138-
final @NotNull Configuration configuration, final @NotNull String loggerName) {
139-
if (LogManager.ROOT_LOGGER_NAME.equals(loggerName)) {
140-
return false;
141-
}
142-
143-
@Nullable String parentLoggerName = getParentLoggerName(loggerName);
144-
while (parentLoggerName != null) {
145-
final LoggerConfig parentLoggerConfig = configuration.getLoggerConfig(parentLoggerName);
146-
if (parentLoggerName.equals(parentLoggerConfig.getName())
147-
&& isSentryAppenderRegistered(parentLoggerConfig)
148-
&& isAdditivePathToAncestor(configuration, loggerName, parentLoggerName)) {
149-
return true;
150-
}
151-
parentLoggerName = getParentLoggerName(parentLoggerName);
152-
}
153-
154-
return isSentryAppenderRegistered(configuration.getRootLogger())
155-
&& isAdditivePathToAncestor(configuration, loggerName, LogManager.ROOT_LOGGER_NAME);
156-
}
157-
158-
private boolean isAdditivePathToAncestor(
159-
final @NotNull Configuration configuration,
160-
final @NotNull String loggerName,
161-
final @NotNull String ancestorLoggerName) {
162-
String currentLoggerName = loggerName;
163-
while (!ancestorLoggerName.equals(currentLoggerName)) {
164-
final LoggerConfig loggerConfig = configuration.getLoggerConfig(currentLoggerName);
165-
if (currentLoggerName.equals(loggerConfig.getName()) && !loggerConfig.isAdditive()) {
166-
return false;
167-
}
168-
currentLoggerName = getParentLoggerName(currentLoggerName);
169-
if (currentLoggerName == null) {
170-
currentLoggerName = LogManager.ROOT_LOGGER_NAME;
171-
}
172-
}
173-
return true;
174-
}
175-
176-
private @Nullable String getParentLoggerName(final @NotNull String loggerName) {
177-
final int separator = loggerName.lastIndexOf('.');
178-
return separator > -1 ? loggerName.substring(0, separator) : null;
179-
}
180-
181-
private boolean isAncestorLogger(
182-
final @NotNull String candidateLoggerName, final @NotNull String loggerName) {
183-
if (candidateLoggerName.equals(loggerName)) {
184-
return false;
114+
normalized.add(normalizeLoggerName(loggerName.trim()));
185115
}
186-
return LogManager.ROOT_LOGGER_NAME.equals(candidateLoggerName)
187-
|| loggerName.startsWith(candidateLoggerName + ".");
116+
return normalized;
188117
}
189118

190119
private boolean isSentryAppenderRegistered(final @NotNull LoggerConfig loggerConfig) {

sentry-spring-boot-4/src/test/kotlin/io/sentry/spring/boot4/SentryLog4j2AppenderAutoConfigurationTest.kt

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,6 @@ class SentryLog4j2AppenderAutoConfigurationTest {
117117
}
118118
}
119119

120-
@Test
121-
fun `does not configure SentryAppender for descendant logger covered by ancestor logger`() {
122-
dsnEnabledRunner
123-
.withPropertyValues("sentry.logging.loggers[0]=ROOT", "sentry.logging.loggers[1]=com.example")
124-
.run {
125-
assertThat(rootLogger.getAppenders(SentryAppender::class.java)).hasSize(1)
126-
assertThat(configuration.loggers).doesNotContainKey("com.example")
127-
}
128-
}
129-
130120
@Test
131121
fun `configures SentryAppender for descendant logger with additivity disabled`() {
132122
val loggerConfig = LoggerConfig("com.example", null, false)

0 commit comments

Comments
 (0)