目前我正在使用 Spring Cache 和 @Cacheable
/@CacheEvict
注释。
我想获得某种控制台日志语句,例如 “INFO:我从缓存中获取了这些值,而不是从主机中获取这些值。太棒了”
有没有一种干净简单的方法来做到这一点?顺便说一句,我们正在使用 slf4j
,如果有任何兴趣的话。
最佳答案
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>
使用此配置,您应该会在控制台上看到类似以下内容:
Cache entry for key 'Page request [number: 0, size 20, sort: null]' found in cache 'persons'
关于java - @Cacheable 命中的 Spring 缓存日志记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37281139/