Search code examples
javaspringspring-bootehcachejcache

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

  • 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