• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

TAKETODAY / today-infrastructure / 18154768944

01 Oct 2025 07:26AM UTC coverage: 81.882% (-0.005%) from 81.887%
18154768944

push

github

web-flow
Merge pull request #290 from TAKETODAY/dev/jspecify

jspecify

59788 of 78013 branches covered (76.64%)

Branch coverage included in aggregate %.

141239 of 167496 relevant lines covered (84.32%)

3.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.3
today-context/src/main/java/infra/cache/interceptor/LoggingCacheErrorHandler.java
1
/*
2
 * Copyright 2017 - 2025 the original author or authors.
3
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see [https://www.gnu.org/licenses/]
16
 */
17

18
package infra.cache.interceptor;
19

20
import org.jspecify.annotations.Nullable;
21

22
import infra.cache.Cache;
23
import infra.lang.Assert;
24
import infra.logging.Logger;
25
import infra.logging.LoggerFactory;
26

27
/**
28
 * A {@link CacheErrorHandler} implementation that logs error message. Can be
29
 * used when underlying cache errors should be ignored.
30
 *
31
 * @author Adam Ostrožlík
32
 * @author Stephane Nicoll
33
 * @author <a href="https://github.com/TAKETODAY">Harry Yang</a>
34
 * @since 4.0
35
 */
36
public class LoggingCacheErrorHandler implements CacheErrorHandler {
37

38
  private final Logger logger;
39

40
  private final boolean logStackTraces;
41

42
  /**
43
   * Create an instance that does not log stack traces.
44
   */
45
  public LoggingCacheErrorHandler() {
46
    this(false);
3✔
47
  }
1✔
48

49
  /**
50
   * Create an instance with the {@link Logger logger} to use.
51
   *
52
   * @param logger the logger to use
53
   * @param logStackTraces whether to log stack trace
54
   */
55
  public LoggingCacheErrorHandler(Logger logger, boolean logStackTraces) {
2✔
56
    Assert.notNull(logger, "Logger is required");
3✔
57
    this.logger = logger;
3✔
58
    this.logStackTraces = logStackTraces;
3✔
59
  }
1✔
60

61
  /**
62
   * Create a {@code LoggingCacheErrorHandler} that uses the default logging
63
   * category and the supplied {@code logStackTraces} flag.
64
   * <p>The default logging category is
65
   * "{@code infra.cache.interceptor.LoggingCacheErrorHandler}".
66
   *
67
   * @param logStackTraces whether to log stack traces
68
   */
69
  public LoggingCacheErrorHandler(boolean logStackTraces) {
70
    this(LoggerFactory.getLogger(LoggingCacheErrorHandler.class), logStackTraces);
5✔
71
  }
1✔
72

73
  /**
74
   * Create a {@code LoggingCacheErrorHandler} that uses the supplied
75
   * {@code loggerName} and {@code logStackTraces} flag.
76
   *
77
   * @param loggerName the logger name to use
78
   * @param logStackTraces whether to log stack traces
79
   */
80
  public LoggingCacheErrorHandler(String loggerName, boolean logStackTraces) {
2✔
81
    Assert.notNull(loggerName, "'loggerName' is required");
3✔
82
    this.logger = LoggerFactory.getLogger(loggerName);
4✔
83
    this.logStackTraces = logStackTraces;
3✔
84
  }
1✔
85

86
  @Override
87
  public void handleCacheGetError(RuntimeException exception, Cache cache, Object key) {
88
    if (logger.isWarnEnabled()) {
4✔
89
      doLogCacheError(logger,
9✔
90
              createMessage(cache, "failed to get entry with key '" + key + "'"),
2✔
91
              exception);
92
    }
93
  }
1✔
94

95
  @Override
96
  public void handleCachePutError(RuntimeException exception, Cache cache, Object key, @Nullable Object value) {
97
    if (logger.isWarnEnabled()) {
4✔
98
      doLogCacheError(logger,
9✔
99
              createMessage(cache, "failed to put entry with key '" + key + "'"),
2✔
100
              exception);
101
    }
102
  }
1✔
103

104
  @Override
105
  public void handleCacheEvictError(RuntimeException exception, Cache cache, Object key) {
106
    if (logger.isWarnEnabled()) {
4!
107
      doLogCacheError(logger,
9✔
108
              createMessage(cache, "failed to evict entry with key '" + key + "'"),
2✔
109
              exception);
110
    }
111
  }
1✔
112

113
  @Override
114
  public void handleCacheClearError(RuntimeException exception, Cache cache) {
115
    if (logger.isWarnEnabled()) {
4!
116
      doLogCacheError(logger, createMessage(cache, "failed to clear entries"), exception);
9✔
117
    }
118
  }
1✔
119

120
  /**
121
   * Get the logger for this {@code LoggingCacheErrorHandler}.
122
   *
123
   * @return the logger
124
   */
125
  protected final Logger getLogger() {
126
    return logger;
×
127
  }
128

129
  /**
130
   * Get the {@code logStackTraces} flag for this {@code LoggingCacheErrorHandler}.
131
   *
132
   * @return {@code true} if this {@code LoggingCacheErrorHandler} logs stack traces
133
   */
134
  protected final boolean isLogStackTraces() {
135
    return this.logStackTraces;
×
136
  }
137

138
  /**
139
   * Log the specified message.
140
   *
141
   * @param logger the logger
142
   * @param message the message
143
   * @param ex the exception
144
   */
145
  protected void doLogCacheError(Logger logger, String message, RuntimeException ex) {
146
    if (logStackTraces) {
3✔
147
      logger.warn(message, ex);
5✔
148
    }
149
    else {
150
      logger.warn(message);
3✔
151
    }
152
  }
1✔
153

154
  private String createMessage(Cache cache, String reason) {
155
    return String.format("Cache '%s' %s", cache.getName(), reason);
14✔
156
  }
157

158
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc