16

In my Spring + Hibernate project, I was doing logging by SLF4J 1.6.4 with LogBack. Now, I've added Ehcache 2.2.0 (through ehcache-spring-annotations-1.1.3). The caching seems to be working as the method, annotated with @Cacheable, no longer being executed, though returning the correct result. But, I'm interested to see the log written by the Ehcache. As Ehcache also uses SLF4J, I supposed, the log should be written into my log file. But, this is not happening. The logback.xml has the following.

 <root level="info">
    <appender-ref ref="STDOUT"/>
    <appender-ref ref="ROLLING"/>
</root>

Adding following also doesn't help

 <logger name="net.sf.ehcache"> 
</logger> 

Ehcache.xml

    <cache name="sampleCache1"
       eternal="false"
       overflowToDisk="true"
       timeToIdleSeconds="300"
       timeToLiveSeconds="600"           
       memoryStoreEvictionPolicy="LFU"           
        />

Please advise me to overcome the problem.

The Ehcache is using SLF4J 1.6.1, while my project was using SLF4J 1.6.4. Can it cause any problem?

Thanks

Tamim
  • 173
  • 1
  • 1
  • 6
  • I just found out that ehcache doesn't log everything I expected. So, have you looked ehcache if it actually logs things you expect? – codesmith Jan 13 '17 at 12:38

4 Answers4

54

EhCache logs a lot on DEBUG level. First of all, this configuration snippet filters out all logging statements below INFO:

<root level="info">

Change it to

<root level="ALL">

Secondly

<logger name="net.sf.ehcache">

needs an increased logging level

<logger name="net.sf.ehcache" level="ALL"/> 

You should then see plenty of logging statements from EhCache (and others).

Bob B
  • 4,484
  • 3
  • 24
  • 32
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • Can somebody please have a look at the quest i posted : http://stackoverflow.com/questions/41734999/spring-framework-application-properties-vs-logback-xml – user18853 Jan 19 '17 at 06:16
14

In my opinion setting the root logger level to ALL is not a good idea.

This means that ALL log messages of every level will get through (unless you set a threshold on your appenders).

Instead, I suggest you set a sensible root logger level, such as INFO or WARN, and then set the log level of individual loggers if you require more specific information.

This way you are not creating a forest of unnecessary information.

I suggest something like the below:

<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender"> 
  <param name="Target" value="System.out"/> 
  <layout class="org.apache.log4j.PatternLayout"> 
    <param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
  </layout>  
</appender> 

<logger name="net.sf.ehcache">
  <level value="DEBUG"/>
</logger>

<root> 
  <priority value ="INFO" /> 
  <appender-ref ref="CONSOLE" /> 
</root>

Adriaan
  • 17,741
  • 7
  • 42
  • 75
Nabil_H
  • 381
  • 3
  • 6
  • Refer http://www.ehcache.org/documentation/2.8/operations/logging.html#recommended-logging-levels – Lucky Jun 04 '18 at 11:29
6

I wrote a custom logger for Ehcache 3 by implementing the CacheEventListener interface as follows

public class CacheLogger implements CacheEventListener<Object, Object> {

    private static final Logger LOG = LoggerFactory.getLogger(CacheLogger.class);

    @Override
    public void onEvent(CacheEvent<?, ?> cacheEvent) {
        LOG.info("YOUR LOG IS HERE");
    }
}

The ehcache.xml will define the listener class

<cache-template name="default">
        <expiry>
            <ttl unit="seconds">300</ttl>
        </expiry>
        <listeners>
            <listener>
                <class>com.path.to.CacheLogger</class>
                <event-firing-mode>ASYNCHRONOUS</event-firing-mode>
                <event-ordering-mode>UNORDERED</event-ordering-mode>
                <events-to-fire-on>CREATED</events-to-fire-on>
                <events-to-fire-on>EXPIRED</events-to-fire-on>
                <events-to-fire-on>EVICTED</events-to-fire-on>
            </listener>
        </listeners>
        <resources>
            <heap>1000</heap>
            <offheap unit="MB">10</offheap>
        </resources>
    </cache-template>
Hany Sakr
  • 2,591
  • 28
  • 27
5

I also found it handy to enable DEBUG logging on org.springframework.cache because I'm using Spring Framework 4.2.1's caching feature (@Cacheable etc.).

ben3000
  • 4,629
  • 6
  • 24
  • 43
  • does that mean adding this line to application.properties: "logging.level.org.springframework.cache: DEBUG" – user18853 Jan 19 '17 at 05:59
  • Yep that would work, but there are [other ways to accomplish it as well](https://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html). – ben3000 Jan 19 '17 at 06:48