Spring cache logging on @Cacheable hit
Asked Answered
T

5

55

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

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"

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

Tightlipped answered 17/5, 2016 at 15:57 Comment(0)
O
59

Spring itself logs some of its Caching Abstractions behaviors under the org.springframework.cache logger in trace level. So, if you append logs under the org.springframework.cache logger 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:

<?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'

Owain answered 17/5, 2016 at 16:24 Comment(1)
that helped me find the similar config in our project setup. thanks :)Tightlipped
Y
74

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

logging.level.org.springframework.cache=TRACE
Yeung answered 11/1, 2019 at 16:11 Comment(0)
O
59

Spring itself logs some of its Caching Abstractions behaviors under the org.springframework.cache logger in trace level. So, if you append logs under the org.springframework.cache logger 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:

<?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'

Owain answered 17/5, 2016 at 16:24 Comment(1)
that helped me find the similar config in our project setup. thanks :)Tightlipped
E
5

if u use application.yml AND spring-boot 2

logging:
  level:
    org.springframework.cache: TRACE

do not set DEBUG level, it works only for TRACE

for application.properties

logging.level.org.springframework.cache=TRACE
Elaboration answered 30/6, 2023 at 7:17 Comment(0)
J
3

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 JMX or spring boot actuator.

To use by JMX, you can refer this

To use Spring Boot Actuator, you can refer this.

Jocular answered 10/3, 2020 at 17:34 Comment(1)
Definitely agree except for if it would be done on local developers machine. However this is very good and valuable hint, how to log and monitor cache behavior.Airliner
S
2

You can enable trace level logging.

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

Spring logging documentation

Somnambulate answered 30/5, 2018 at 14:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.