Skip to content

Commit fb29137

Browse files
vpavicjhoeller
authored andcommitted
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.
1 parent 182ba4a commit fb29137

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

spring-context/src/main/java/org/springframework/cache/interceptor/LoggingCacheErrorHandler.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,19 @@ public LoggingCacheErrorHandler(Log logger, boolean logStackTraces) {
7777
this.logStackTraces = logStackTraces;
7878
}
7979

80+
/**
81+
* Create a {@code LoggingCacheErrorHandler} that uses the supplied
82+
* {@code loggerName} and {@code logStackTraces} flag.
83+
* @param loggerName the logger name to use
84+
* @param logStackTraces whether to log stack traces
85+
* @since 5.3.24
86+
*/
87+
public LoggingCacheErrorHandler(String loggerName, boolean logStackTraces) {
88+
Assert.notNull(loggerName, "'loggerName' must not be null");
89+
this.logger = LogFactory.getLog(loggerName);
90+
this.logStackTraces = logStackTraces;
91+
}
92+
8093

8194
@Override
8295
public void handleCacheGetError(RuntimeException exception, Cache cache, Object key) {

spring-context/src/test/java/org/springframework/cache/interceptor/LoggingCacheErrorHandlerTests.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.cache.Cache;
2424
import org.springframework.cache.support.NoOpCache;
2525

26+
import static org.assertj.core.api.Assertions.assertThatCode;
2627
import static org.mockito.BDDMockito.given;
2728
import static org.mockito.Mockito.mock;
2829
import static org.mockito.Mockito.verify;
@@ -84,4 +85,10 @@ void handleGetCacheErrorWithStackTraceLoggingEnabled() {
8485
verify(this.logger).warn("Cache 'NOOP' failed to get entry with key 'enigma'", exception);
8586
}
8687

88+
@Test
89+
void constructorWithLoggerName() {
90+
assertThatCode(() -> new LoggingCacheErrorHandler("org.apache.commons.logging.Log", true))
91+
.doesNotThrowAnyException();
92+
}
93+
8794
}

0 commit comments

Comments
 (0)