Java @Cacheable 命中的 Spring 缓存日志记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37281139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:08:40  来源:igfitidea点击:

Spring cache logging on @Cacheable hit

javaspringslf4jspring-cache

提问by BassSultan

Currently I am working with a Spring Cache and the @Cacheable/@CacheEvictannotations.

目前我正在使用 Spring Cache 和@Cacheable/@CacheEvict注释。

I would like to get some sort of a console log statement like "INFO: i got those values from the cache, NOT from the host. awesome"

我想获得某种控制台日志语句,例如 "INFO: i got those values from the cache, NOT from the host. awesome"

Is there a clean and easy way to do this? We are using slf4japparently btw, if that is of any interest.

有没有一种干净简单的方法来做到这一点?我们slf4j显然正在使用btw,如果有任何兴趣的话。

采纳答案by Ali Dehghani

Spring itself logs some of its Caching Abstractions behaviors under the org.springframework.cachelogger in tracelevel. So, if you append logs under the org.springframework.cachelogger to an appropriate appender, you would have some useful information on, say, the console. If you're using Logback, you could use something like the following in your logback.xml:

Spring 本身org.springframework.cachetrace级别的记录器下记录了它的一些缓存抽象行为。因此,如果您将日志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>

With this configuration, you should see something like following on your console:

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

Cache entry for key 'Page request [number: 0, size 20, sort: null]' found in cache 'persons'

在缓存 'persons' 中找到键 'Page request [number: 0, size 20, sort: null]' 的缓存条目

回答by Mate ?imovi?

You can enable trace level logging.

您可以启用跟踪级别日志记录。

Eg., in application.properties put 'trace=true'.

例如,在 application.properties 中放置 'trace=true'。

Spring logging documentation

Spring日志记录文档

回答by Rafael Renan Pacheco

And for Spring Boot 2 you can add in your application.properties:

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

logging.level.org.springframework.cache=TRACE

回答by Mavlarn

I don't think it is a good idea to open trace log all the time, even it is only for cache log.
The better way is, EHcache has this hit/miss metrics already, you can get it by JMXor spring boot actuator.

我不认为一直打开跟踪日志是一个好主意,即使它只用于缓存日志。
更好的方法是,EHcache 已经有了这个命中/未命中指标,您可以通过JMX或获取它spring boot actuator

To use by JMX, you can refer this

要由 JMX 使用,您可以参考这个

To use Spring Boot Actuator, you can refer this.

要使用Spring Boot Actuator,你可以参考这个