Skip to content

Commit 26df458

Browse files
committed
Revise internals of LoggingCacheErrorHandler
Since LoggingCacheErrorHandler was only recently introduced in 5.3.16, we have decided to completely revise its internals (protected API) in 5.3.x while retaining the current public API. Specifically, this commit: - introduces protected getLogger() and isLogStackTraces() methods to improve extensibility - revises logCacheError() to accept a Supplier<String> for lazy resolution of error messages Closes gh-28672 See gh-28670, gh-28648
1 parent 57208bf commit 26df458

File tree

2 files changed

+75
-41
lines changed

2 files changed

+75
-41
lines changed

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

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.cache.interceptor;
1818

19+
import java.util.function.Supplier;
20+
1921
import org.apache.commons.logging.Log;
2022
import org.apache.commons.logging.LogFactory;
2123

@@ -78,47 +80,71 @@ public LoggingCacheErrorHandler(Log logger, boolean logStackTraces) {
7880

7981
@Override
8082
public void handleCacheGetError(RuntimeException exception, Cache cache, Object key) {
81-
logCacheError(logger,
82-
createMessage(cache, "failed to get entry with key '" + key + "'"),
83+
logCacheError(
84+
() -> String.format("Cache '%s' failed to get entry with key '%s'", cache.getName(), key),
8385
exception);
8486
}
8587

8688
@Override
8789
public void handleCachePutError(RuntimeException exception, Cache cache, Object key, @Nullable Object value) {
88-
logCacheError(logger,
89-
createMessage(cache, "failed to put entry with key '" + key + "'"),
90+
logCacheError(
91+
() -> String.format("Cache '%s' failed to put entry with key '%s'", cache.getName(), key),
9092
exception);
9193
}
9294

9395
@Override
9496
public void handleCacheEvictError(RuntimeException exception, Cache cache, Object key) {
95-
logCacheError(logger,
96-
createMessage(cache, "failed to evict entry with key '" + key + "'"),
97+
logCacheError(
98+
() -> String.format("Cache '%s' failed to evict entry with key '%s'", cache.getName(), key),
9799
exception);
98100
}
99101

100102
@Override
101103
public void handleCacheClearError(RuntimeException exception, Cache cache) {
102-
logCacheError(logger, createMessage(cache, "failed to clear entries"), exception);
104+
logCacheError(
105+
() -> String.format("Cache '%s' failed to clear entries", cache.getName()),
106+
exception);
103107
}
104108

109+
105110
/**
106-
* Log the specified message.
107-
* @param logger the logger
108-
* @param message the message
109-
* @param ex the exception
111+
* Get the logger for this {@code LoggingCacheErrorHandler}.
112+
* @return the logger
113+
* @since 5.3.22
110114
*/
111-
protected void logCacheError(Log logger, String message, RuntimeException ex) {
112-
if (this.logStackTraces) {
113-
logger.warn(message, ex);
114-
}
115-
else {
116-
logger.warn(message);
117-
}
115+
protected final Log getLogger() {
116+
return logger;
118117
}
119118

120-
private String createMessage(Cache cache, String reason) {
121-
return String.format("Cache '%s' %s", cache.getName(), reason);
119+
/**
120+
* Get the {@code logStackTraces} flag for this {@code LoggingCacheErrorHandler}.
121+
* @return {@code true} if this {@code LoggingCacheErrorHandler} logs stack traces
122+
* @since 5.3.22
123+
*/
124+
protected final boolean isLogStackTraces() {
125+
return this.logStackTraces;
126+
}
127+
128+
/**
129+
* Log the cache error message in the given supplier.
130+
* <p>If {@link #isLogStackTraces()} is {@code true}, the given
131+
* {@code exception} will be logged as well.
132+
* <p>The default implementation logs the message as a warning.
133+
* @param messageSupplier the message supplier
134+
* @param exception the exception thrown by the cache provider
135+
* @since 5.3.22
136+
* @see #isLogStackTraces()
137+
* @see #getLogger()
138+
*/
139+
protected void logCacheError(Supplier<String> messageSupplier, RuntimeException exception) {
140+
if (getLogger().isWarnEnabled()) {
141+
if (isLogStackTraces()) {
142+
getLogger().warn(messageSupplier.get(), exception);
143+
}
144+
else {
145+
getLogger().warn(messageSupplier.get());
146+
}
147+
}
122148
}
123149

124150
}

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

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@
1717
package org.springframework.cache.interceptor;
1818

1919
import org.apache.commons.logging.Log;
20+
import org.junit.jupiter.api.BeforeEach;
2021
import org.junit.jupiter.api.Test;
21-
import org.junit.jupiter.api.extension.ExtendWith;
22-
import org.mockito.Mock;
23-
import org.mockito.junit.jupiter.MockitoExtension;
2422

23+
import org.springframework.cache.Cache;
2524
import org.springframework.cache.support.NoOpCache;
2625

26+
import static org.mockito.BDDMockito.given;
27+
import static org.mockito.Mockito.mock;
2728
import static org.mockito.Mockito.verify;
2829

2930
/**
@@ -32,48 +33,55 @@
3233
* @author Adam Ostrožlík
3334
* @author Stephane Nicoll
3435
* @author Vedran Pavic
36+
* @author Sam Brannen
3537
*/
36-
@ExtendWith(MockitoExtension.class)
3738
class LoggingCacheErrorHandlerTests {
3839

39-
@Mock
40-
private Log logger;
40+
private static final Cache CACHE = new NoOpCache("NOOP");
41+
42+
private static final String KEY = "enigma";
43+
44+
private final Log logger = mock(Log.class);
45+
46+
private LoggingCacheErrorHandler handler = new LoggingCacheErrorHandler(this.logger, false);
47+
48+
49+
@BeforeEach
50+
void setUp() {
51+
given(this.logger.isWarnEnabled()).willReturn(true);
52+
}
4153

4254

4355
@Test
4456
void handleGetCacheErrorLogsAppropriateMessage() {
45-
LoggingCacheErrorHandler handler = new LoggingCacheErrorHandler(this.logger, false);
46-
handler.handleCacheGetError(new RuntimeException(), new NoOpCache("NOOP"), "key");
47-
verify(this.logger).warn("Cache 'NOOP' failed to get entry with key 'key'");
57+
this.handler.handleCacheGetError(new RuntimeException(), CACHE, KEY);
58+
verify(this.logger).warn("Cache 'NOOP' failed to get entry with key 'enigma'");
4859
}
4960

5061
@Test
5162
void handlePutCacheErrorLogsAppropriateMessage() {
52-
LoggingCacheErrorHandler handler = new LoggingCacheErrorHandler(this.logger, false);
53-
handler.handleCachePutError(new RuntimeException(), new NoOpCache("NOOP"), "key", new Object());
54-
verify(this.logger).warn("Cache 'NOOP' failed to put entry with key 'key'");
63+
this.handler.handleCachePutError(new RuntimeException(), CACHE, KEY, null);
64+
verify(this.logger).warn("Cache 'NOOP' failed to put entry with key 'enigma'");
5565
}
5666

5767
@Test
5868
void handleEvictCacheErrorLogsAppropriateMessage() {
59-
LoggingCacheErrorHandler handler = new LoggingCacheErrorHandler(this.logger, false);
60-
handler.handleCacheEvictError(new RuntimeException(), new NoOpCache("NOOP"), "key");
61-
verify(this.logger).warn("Cache 'NOOP' failed to evict entry with key 'key'");
69+
this.handler.handleCacheEvictError(new RuntimeException(), CACHE, KEY);
70+
verify(this.logger).warn("Cache 'NOOP' failed to evict entry with key 'enigma'");
6271
}
6372

6473
@Test
6574
void handleClearErrorLogsAppropriateMessage() {
66-
LoggingCacheErrorHandler handler = new LoggingCacheErrorHandler(this.logger, false);
67-
handler.handleCacheClearError(new RuntimeException(), new NoOpCache("NOOP"));
75+
this.handler.handleCacheClearError(new RuntimeException(), CACHE);
6876
verify(this.logger).warn("Cache 'NOOP' failed to clear entries");
6977
}
7078

7179
@Test
72-
void handleCacheErrorWithStacktrace() {
73-
LoggingCacheErrorHandler handler = new LoggingCacheErrorHandler(this.logger, true);
80+
void handleGetCacheErrorWithStackTraceLoggingEnabled() {
81+
this.handler = new LoggingCacheErrorHandler(this.logger, true);
7482
RuntimeException exception = new RuntimeException();
75-
handler.handleCacheGetError(exception, new NoOpCache("NOOP"), "key");
76-
verify(this.logger).warn("Cache 'NOOP' failed to get entry with key 'key'", exception);
83+
this.handler.handleCacheGetError(exception, CACHE, KEY);
84+
verify(this.logger).warn("Cache 'NOOP' failed to get entry with key 'enigma'", exception);
7785
}
7886

7987
}

0 commit comments

Comments
 (0)