'How to log cache hits through @Cacheable annotation?

There is a method in a class like this down below :

    @Override
    @Transactional
    @Cacheable(value = "products", key = "#id")
    public Product getProduct(long id) throws ApplicationException {
        Product product = null;
        try {
            ProductEntity productEntity = productDAO.getProduct(id);
            product = productTransformer.toProduct(productEntity);
        } catch (SystemException ex) {
            throw new ApplicationException(ex.getCode(), ex.getMessage(), "Problem in DataLayer", "Data Layer Error",
                    new Object[] { ex });
        }
        return product;
    }

The application is working fine. But I want to have a cache hit log when data is put into the cache. I want to log it through log4j.properties.

How can I configure the application.properties in such a way so that it can be logged ?



Solution 1:[1]

Spring internally logs its caching workflow at a TRACE level. To enable this , in your application.properties file, include the following.

logging.level.org.springframework.cache=TRACE

Solution 2:[2]

Including the following should help:

logging.level.org.springframework.cache=TRACE

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dehan
Solution 2