Skip to content

Commit

Permalink
Avoid use of Commons Logging in LoggingCacheErrorHandler public API
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vpavic authored and jhoeller committed Oct 19, 2022
1 parent 182ba4a commit fb29137
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
Expand Up @@ -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) {
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

}

0 comments on commit fb29137

Please sign in to comment.