From fa8e2e96b1569171ecfa49861d4558a2b0b85f5b Mon Sep 17 00:00:00 2001 From: Vedran Pavic Date: Wed, 22 Jun 2022 17:44:57 +0200 Subject: [PATCH] Avoid use of Commons Logging in LoggingCacheErrorHandler public API At present, creating LoggingCacheErrorHandler with custom logger requires use of Commons Logging API, as the appropriate constructor expects org.apache.commons.logging.Log instance. As Commons Logging is rarely the logging framework of choice in applications these days, interaction with its API might not be desirable. This commit adds LoggingCacheErrorHandler constructor that accepts logger name and thus avoids leaking out any details about the underlying logging framework. --- .../cache/interceptor/LoggingCacheErrorHandler.java | 13 +++++++++++++ .../interceptor/LoggingCacheErrorHandlerTests.java | 7 +++++++ 2 files changed, 20 insertions(+) diff --git a/spring-context/src/main/java/org/springframework/cache/interceptor/LoggingCacheErrorHandler.java b/spring-context/src/main/java/org/springframework/cache/interceptor/LoggingCacheErrorHandler.java index bfb821e350d8..a8943ec98564 100644 --- a/spring-context/src/main/java/org/springframework/cache/interceptor/LoggingCacheErrorHandler.java +++ b/spring-context/src/main/java/org/springframework/cache/interceptor/LoggingCacheErrorHandler.java @@ -77,6 +77,19 @@ public LoggingCacheErrorHandler(Log logger, boolean logStackTraces) { this.logStackTraces = logStackTraces; } + /** + * Create a {@code LoggingCacheErrorHandler} that uses the supplied + * {@code loggerName} and {@code logStackTraces} flag. + * @param loggerName the logger name to use + * @param logStackTraces whether to log stack traces + * @since 5.3.24 + */ + public LoggingCacheErrorHandler(String loggerName, boolean logStackTraces) { + Assert.notNull(loggerName, "'loggerName' must not be null"); + this.logger = LogFactory.getLog(loggerName); + this.logStackTraces = logStackTraces; + } + @Override public void handleCacheGetError(RuntimeException exception, Cache cache, Object key) { diff --git a/spring-context/src/test/java/org/springframework/cache/interceptor/LoggingCacheErrorHandlerTests.java b/spring-context/src/test/java/org/springframework/cache/interceptor/LoggingCacheErrorHandlerTests.java index e9dc06416942..b96e10b70121 100644 --- a/spring-context/src/test/java/org/springframework/cache/interceptor/LoggingCacheErrorHandlerTests.java +++ b/spring-context/src/test/java/org/springframework/cache/interceptor/LoggingCacheErrorHandlerTests.java @@ -23,6 +23,7 @@ import org.springframework.cache.Cache; import org.springframework.cache.support.NoOpCache; +import static org.assertj.core.api.Assertions.assertThatCode; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -84,4 +85,10 @@ void handleGetCacheErrorWithStackTraceLoggingEnabled() { verify(this.logger).warn("Cache 'NOOP' failed to get entry with key 'enigma'", exception); } + @Test + void constructorWithLoggerName() { + assertThatCode(() -> new LoggingCacheErrorHandler("org.apache.commons.logging.Log", true)) + .doesNotThrowAnyException(); + } + }