Skip to content

Commit 9390fe5

Browse files
author
Adam Ostrožlík
committed
CacheErrorHandler implementation which simply logs exception.
1 parent e1acbca commit 9390fe5

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2002-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cache.interceptor;
18+
19+
import org.apache.commons.logging.Log;
20+
import org.apache.commons.logging.LogFactory;
21+
22+
import org.springframework.cache.Cache;
23+
import org.springframework.lang.Nullable;
24+
25+
/**
26+
* A {@link CacheErrorHandler} implementation that simply logs exception.
27+
*
28+
* @author Adam Ostrožlík
29+
* @since 5.3
30+
*/
31+
public class LoggingCacheErrorHandler implements CacheErrorHandler {
32+
33+
protected static final Log logger = LogFactory.getLog(LoggingCacheErrorHandler.class);
34+
private final boolean includeStacktrace;
35+
36+
/**
37+
* Construct new {@link LoggingCacheErrorHandler} that does not log stacktraces.
38+
*/
39+
public LoggingCacheErrorHandler() {
40+
this.includeStacktrace = false;
41+
}
42+
43+
/**
44+
* Construct new {@link LoggingCacheErrorHandler} that may log stacktraces.
45+
* @param includeStacktrace whether to log or not log stacktraces.
46+
*/
47+
public LoggingCacheErrorHandler(boolean includeStacktrace) {
48+
this.includeStacktrace = includeStacktrace;
49+
}
50+
51+
@Override
52+
public void handleCacheGetError(RuntimeException exception, Cache cache, Object key) {
53+
logCacheError("Cache get error for key " + key + " and cache " + cache.getName(), exception);
54+
}
55+
56+
@Override
57+
public void handleCachePutError(RuntimeException exception, Cache cache, Object key, @Nullable Object value) {
58+
logCacheError("Cache put error for key " + key + " and cache " + cache.getName(), exception);
59+
}
60+
61+
@Override
62+
public void handleCacheEvictError(RuntimeException exception, Cache cache, Object key) {
63+
logCacheError("Cache evict error for key " + key + " and cache " + cache.getName(), exception);
64+
}
65+
66+
@Override
67+
public void handleCacheClearError(RuntimeException exception, Cache cache) {
68+
logCacheError("Cache clear error for cache " + cache.getName(), exception);
69+
}
70+
71+
public boolean isIncludeStacktrace() {
72+
return this.includeStacktrace;
73+
}
74+
75+
protected void logCacheError(String msg, RuntimeException ex) {
76+
if (isIncludeStacktrace()) {
77+
logger.warn(msg, ex);
78+
}
79+
else {
80+
logger.warn(msg);
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)