micronaut-projects / micronaut-cache

This project includes caching support for Micronaut
Apache License 2.0
29 stars 31 forks source link

@Slf4j breaks with cache annotations #655

Open j1m-renwick opened 1 year ago

j1m-renwick commented 1 year ago

Expected Behavior

the @Slf4j log property to be available in all parts of a class that features cache-related annotations

Actual Behaviour

the @SLF4J log property is sometimes not found in methods that belong to classes that feature cache-related annotations

Steps To Reproduce

  1. run cache-log-bug.zip
  2. observe the error message: groovy.lang.MissingPropertyException: No such property: log for class: com.example.$MyCachedClass$Definition$Intercepted - either by running the unit tests, or by calling the GET APIs featured in MyController.

( NOTE: this seems to occur for other annotations like @CacheInvalidate as well - not just @Cacheable )

Environment Information

Operating System: MacOS Ventura, Version 13.4 JDK Version: Coretto 17

Example Application

see above

Version

4.0.5

j1m-renwick commented 1 year ago

Update: looks like setting a logger explicitly in the class instead of the SLF4J annotation works

e.g. static Logger log = LoggerFactory.getLogger(MyClass) (note the lack of a private keyword)

timyates commented 8 months ago

Another workaround is to capture the logger in the @Cacheable method, and then use that in the closure

    @Cacheable(cacheNames = "some-cache")
    Single<String> myCachedMethodWithInnerLogging() {
        def logger = log
        Single.just("hi")
            .map{
                logger.info("saying hi!")
                return it
            }
    }

and another is to provide a getter for the log variable

    def getLog() {
        return log
    }