Package org.springframework.cache.interceptor

# Class LoggingCacheErrorHandler

java.lang.Object
org.springframework.cache.interceptor.LoggingCacheErrorHandler

All Implemented Interfaces:
  `CacheErrorHandler`

public class LoggingCacheErrorHandler extends Object implements CacheErrorHandler
A `CacheErrorHandler` implementation that logs error messages.

Can be used when underlying cache errors should be ignored.

Since:
  5.3.16
Author:
  Adam Ostrožlík, Stephane Nicoll, Vedran Pavic, Sam Brannen

## Constructor Summary

Constructors
Constructor
Description
`LoggingCacheErrorHandler()`
Create a `LoggingCacheErrorHandler` that uses the default logging category and does not log stack traces.
`LoggingCacheErrorHandler(boolean logStackTraces)`
Create a `LoggingCacheErrorHandler` that uses the default logging category and the supplied `logStackTraces` flag.
`LoggingCacheErrorHandler(String loggerName, boolean logStackTraces)`
Create a `LoggingCacheErrorHandler` that uses the supplied `loggerName` and `logStackTraces` flag.
`LoggingCacheErrorHandler(org.apache.commons.logging.Log logger, boolean logStackTraces)`
Create a `LoggingCacheErrorHandler` that uses the supplied `logger` and `logStackTraces` flag.

## Method Summary

Modifier and Type
Method
Description
`protected final org.apache.commons.logging.Log`
`getLogger()`
Get the logger for this `LoggingCacheErrorHandler`.
`void`
`handleCacheClearError(RuntimeException exception, Cache cache)`
Handle the given runtime exception thrown by the cache provider when clearing the specified `Cache`, possibly rethrowing it as a fatal exception.
`void`
`handleCacheEvictError(RuntimeException exception, Cache cache, Object key)`
Handle the given runtime exception thrown by the cache provider when clearing an item with the specified `key`, possibly rethrowing it as a fatal exception.
`void`
`handleCacheGetError(RuntimeException exception, Cache cache, Object key)`
Handle the given runtime exception thrown by the cache provider when retrieving an item with the specified `key`, possibly rethrowing it as a fatal exception.
`void`
`handleCachePutError(RuntimeException exception, Cache cache, Object key, Object value)`
Handle the given runtime exception thrown by the cache provider when updating an item with the specified `key` and `value`, possibly rethrowing it as a fatal exception.
`protected final boolean`
`isLogStackTraces()`
Get the `logStackTraces` flag for this `LoggingCacheErrorHandler`.
`protected void`
`logCacheError(Supplier<String> messageSupplier, RuntimeException exception)`
Log the cache error message in the given supplier.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### LoggingCacheErrorHandler

public LoggingCacheErrorHandler()
Create a `LoggingCacheErrorHandler` that uses the default logging category and does not log stack traces.

The default logging category is "`org.springframework.cache.interceptor.LoggingCacheErrorHandler`".

### LoggingCacheErrorHandler

public LoggingCacheErrorHandler(boolean logStackTraces)
Create a `LoggingCacheErrorHandler` that uses the default logging category and the supplied `logStackTraces` flag.

The default logging category is "`org.springframework.cache.interceptor.LoggingCacheErrorHandler`".

Parameters:
  `logStackTraces` - whether to log stack traces
Since:
  5.3.22

### LoggingCacheErrorHandler

public LoggingCacheErrorHandler(org.apache.commons.logging.Log logger, boolean logStackTraces)
Create a `LoggingCacheErrorHandler` that uses the supplied `logger` and `logStackTraces` flag.

Parameters:
  `logger` - the logger to use
  `logStackTraces` - whether to log stack traces

### LoggingCacheErrorHandler

public LoggingCacheErrorHandler(String loggerName, boolean logStackTraces)
Create a `LoggingCacheErrorHandler` that uses the supplied `loggerName` and `logStackTraces` flag.

Parameters:
  `loggerName` - the name of the logger to use. The name will be passed to the underlying logger implementation through Commons Logging, getting interpreted as log category according to the logger's configuration.
  `logStackTraces` - whether to log stack traces
Since:
  5.3.24
See Also:
  - `LogFactory.getLog(String)`
  - `Logger.getLogger(String)`

## Method Details

### handleCacheGetError

public void handleCacheGetError(RuntimeException exception, Cache cache, Object key)
Description copied from interface: `CacheErrorHandler`
Handle the given runtime exception thrown by the cache provider when retrieving an item with the specified `key`, possibly rethrowing it as a fatal exception.

Note that for a default `@Cacheable` setup, this will be called after an initial cache access failure, whereas the subsequent put step may independently fail and be handled in `CacheErrorHandler.handleCachePutError(java.lang.RuntimeException, org.springframework.cache.Cache, java.lang.Object, java.lang.Object)` still. However, for `@Cacheable(sync=true)`, there is only a combined get step with `handleCacheGetError` being called in case of failure; there won't be a separate put attempt after initial cache access failure anymore.

Specified by:
  `handleCacheGetError` in interface `CacheErrorHandler`
Parameters:
  `exception` - the exception thrown by the cache provider
  `cache` - the cache
  `key` - the key used to get the item
See Also:
  - `Cache.get(Object)`
  - `Cache.get(Object, Callable)`

### handleCachePutError

public void handleCachePutError(RuntimeException exception, Cache cache, Object key, @Nullable Object value)
Description copied from interface: `CacheErrorHandler`
Handle the given runtime exception thrown by the cache provider when updating an item with the specified `key` and `value`, possibly rethrowing it as a fatal exception.

Specified by:
  `handleCachePutError` in interface `CacheErrorHandler`
Parameters:
  `exception` - the exception thrown by the cache provider
  `cache` - the cache
  `key` - the key used to update the item
  `value` - the value to associate with the key
See Also:
  - `Cache.put(Object, Object)`

### handleCacheEvictError

public void handleCacheEvictError(RuntimeException exception, Cache cache, Object key)
Description copied from interface: `CacheErrorHandler`
Handle the given runtime exception thrown by the cache provider when clearing an item with the specified `key`, possibly rethrowing it as a fatal exception.

Specified by:
  `handleCacheEvictError` in interface `CacheErrorHandler`
Parameters:
  `exception` - the exception thrown by the cache provider
  `cache` - the cache
  `key` - the key used to clear the item

### handleCacheClearError

public void handleCacheClearError(RuntimeException exception, Cache cache)
Description copied from interface: `CacheErrorHandler`
Handle the given runtime exception thrown by the cache provider when clearing the specified `Cache`, possibly rethrowing it as a fatal exception.

Specified by:
  `handleCacheClearError` in interface `CacheErrorHandler`
Parameters:
  `exception` - the exception thrown by the cache provider
  `cache` - the cache to clear

### getLogger

protected final org.apache.commons.logging.Log getLogger()
Get the logger for this `LoggingCacheErrorHandler`.

Returns:
  the logger
Since:
  5.3.22

### isLogStackTraces

protected final boolean isLogStackTraces()
Get the `logStackTraces` flag for this `LoggingCacheErrorHandler`.

Returns:
  `true` if this `LoggingCacheErrorHandler` logs stack traces
Since:
  5.3.22

### logCacheError

protected void logCacheError(Supplier<String> messageSupplier, RuntimeException exception)
Log the cache error message in the given supplier.

If `isLogStackTraces()` is `true`, the given `exception` will be logged as well.

The default implementation logs the message as a warning.

Parameters:
  `messageSupplier` - the message supplier
  `exception` - the exception thrown by the cache provider
Since:
  5.3.22
See Also:
  - `isLogStackTraces()`
  - `getLogger()`