如何启用Ehcache的日志记录

Tam*_*mim 12 spring ehcache logback slf4j

在我的Spring + Hibernate项目中,我使用LogBack通过SLF4J 1.6.4进行日志记录.现在,我添加了Ehcache 2.2.0(通过ehcache-spring-annotations-1.1.3).缓存似乎是作为方法工作,使用@Cacheable注释,不再执行,但返回正确的结果.但是,我有兴趣看到Ehcache写的日志.由于Ehcache也使用SLF4J,我想,日志应该写入我的日志文件中.但是,这种情况并没有发生.logback.xml具有以下内容.

 <root level="info">
    <appender-ref ref="STDOUT"/>
    <appender-ref ref="ROLLING"/>
</root>
Run Code Online (Sandbox Code Playgroud)

添加以下内容也无济于事

 <logger name="net.sf.ehcache"> 
</logger> 
Run Code Online (Sandbox Code Playgroud)

ehcache.xml中

    <cache name="sampleCache1"
       eternal="false"
       overflowToDisk="true"
       timeToIdleSeconds="300"
       timeToLiveSeconds="600"           
       memoryStoreEvictionPolicy="LFU"           
        />
Run Code Online (Sandbox Code Playgroud)

请告诉我克服这个问题.

Ehcache正在使用SLF4J 1.6.1,而我的项目正在使用SLF4J 1.6.4.它会引起任何问题吗?

谢谢

Tom*_*icz 52

EhCache在DEBUG关卡上记录了很多.首先,此配置代码段会过滤掉以下所有日志记录语句INFO:

<root level="info">
Run Code Online (Sandbox Code Playgroud)

将其更改为

<root level="ALL">
Run Code Online (Sandbox Code Playgroud)

其次

<logger name="net.sf.ehcache">
Run Code Online (Sandbox Code Playgroud)

需要提高日志记录级别

<logger name="net.sf.ehcache" level="ALL"/> 
Run Code Online (Sandbox Code Playgroud)

然后,您应该看到EhCache(和其他人)的大量日志记录.

  • 抱歉没有足够的声誉投票:) (3认同)

Nab*_*l_H 10

在我看来,将根记录器级别设置ALL为不是一个好主意.

这意味着ALL每个级别的日志消息都将通过(除非您在appender上设置阈值).

相反,我建议您设置合理的根记录器级别,例如INFOWARN,然后在需要更多特定信息时设置各个记录器的日志级别.

这样您就不会创建不必要信息的森林.

我建议如下:

<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>
Run Code Online (Sandbox Code Playgroud)


Han*_*akr 6

我通过实现 CacheEventListener 接口为 Ehcache 3 编写了一个自定义记录器,如下所示

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");
    }
}
Run Code Online (Sandbox Code Playgroud)

ehcache.xml将定义监听器类

<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>
Run Code Online (Sandbox Code Playgroud)


ben*_*000 5

我还发现启用DEBUG登录很方便,org.springframework.cache因为我使用的是Spring Framework 4.2.1的缓存功能(@Cacheable等).