目前我正在使用Spring Cache和@Cacheable
/@CacheEvict
注解。
我希望能够得到一些类似于"INFO: 我从缓存中获取这些值,而不是从主机上获取。太棒了"
的控制台日志。
是否有一种简洁易行的方法来实现这个?我们似乎在使用slf4j
,如果这有任何意义的话。
目前我正在使用Spring Cache和@Cacheable
/@CacheEvict
注解。
我希望能够得到一些类似于"INFO: 我从缓存中获取这些值,而不是从主机上获取。太棒了"
的控制台日志。
是否有一种简洁易行的方法来实现这个?我们似乎在使用slf4j
,如果这有任何意义的话。
对于Spring Boot 2,您可以在application.properties文件中添加以下内容:
logging.level.org.springframework.cache=TRACE
Spring本身在trace
级别下记录了一些缓存抽象行为,这些记录可在org.springframework.cache
记录器下找到。因此,如果您将记录附加到适当的appender下的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: null]”的缓存条目,缓存名称为“persons”
logging:
level:
org.springframework.cache: TRACE
不要设置 DEBUG 级别,它仅适用于 TRACE。
对于 application.properties 文件。
logging.level.org.springframework.cache=TRACE