-
-
Notifications
You must be signed in to change notification settings - Fork 475
log4j spring boot 4 autoinstrumentation #5403
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lbloder
wants to merge
7
commits into
main
Choose a base branch
from
feat/log4j-autoinstrumentation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4e03272
initial log4j appender auto config
lbloder 4536c32
Improve Log4j2 appender auto-configuration tests
lbloder a3d7b83
Merge branch 'main' into feat/log4j-autoinstrumentation
lbloder d896d43
remove ancestor safeguard to match logback, as the user defines which…
lbloder 7d76aa5
add changelog
lbloder 2493682
Merge branch 'main' into feat/log4j-autoinstrumentation
lbloder 70939c1
move changelog entry to unrelease section
lbloder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
...ng-boot-4/src/main/java/io/sentry/spring/boot4/SentryLog4j2AppenderAutoConfiguration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
134 changes: 134 additions & 0 deletions
134
sentry-spring-boot-4/src/main/java/io/sentry/spring/boot4/SentryLog4j2Initializer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()); | ||
| } | ||
| } |
1 change: 1 addition & 0 deletions
1
...esources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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-log4j2pluslog4j-corewhile 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.Reviewed by Cursor Bugbot for commit 2493682. Configure here.