在@Cacheable Hit上登录Spring缓存 [英] Spring cache logging on @Cacheable hit

查看:36
本文介绍了在@Cacheable Hit上登录Spring缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前我正在使用一个Spring缓存和@Cacheable/@CacheEvict批注。

我希望获得类似"INFO: i got those values from the cache, NOT from the host. awesome"

的某种控制台日志语句

有没有一种简单明了的方法来做到这一点?我们使用的显然是slf4jbtw,如果您对此感兴趣的话。

Spring

推荐答案本身在trace级别的org.springframework.cache记录器下记录它的一些缓存抽象行为。因此,如果您将org.springframework.cache记录器下的日志附加到适当的附加器,就会有一些关于控制台的有用信息。如果您使用的是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>

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

关键字‘Page Request[Number:0,Size 20,Sort:空]’的缓存条目 在缓存‘Person’中找到

这篇关于在@Cacheable Hit上登录Spring缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆