Spring缓存登录@Cacheable命中

Bas*_*tan 19 java spring slf4j spring-cache

目前我正在使用Spring Cache和@Cacheable/ @CacheEvictannotations.

我想得到某种类似的控制台日志声明 "INFO: i got those values from the cache, NOT from the host. awesome"

有一个干净,简单的方法吗?slf4j如果有任何兴趣,我们显然使用btw.

Ali*_*ani 34

Spring本身在org.springframework.cache记录器的trace级别下记录了一些缓存抽象行为.因此,如果您将记录org.springframework.cache器下的日志附加到适当的appender,那么您可以在控制台上获得一些有用的信息.如果您正在使用Logback,则可以在以下内容中使用以下内容logback.xml:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%msg%n</pattern>
        </encoder>
    </appender>

    <logger name="org.springframework.cache" level="trace">
        <appender-ref ref="STDOUT" />
    </logger>
</configuration>
Run Code Online (Sandbox Code Playgroud)

使用此配置,您应该在控制台上看到以下内容:

缓存"人员"中找到的密钥"页面请求[编号:0,大小20,排序:空]"的缓存条目


Raf*_*eco 30

对于 Spring Boot 2,您可以在 application.properties 中添加:

logging.level.org.springframework.cache=TRACE
Run Code Online (Sandbox Code Playgroud)