【问题标题】:How to enable logging of Ehcache如何启用 Ehcache 的日志记录
【发布时间】:2012-05-22 15:25:27
【问题描述】:

在我的 Spring + Hibernate 项目中,我使用 SLF4J 1.6.4 和 LogBack 进行日志记录。现在,我添加了 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>

添加关注也无济于事

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

Ehcache.xml

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

请建议我解决这个问题。

Ehcache 使用的是 SLF4J 1.6.1,而我的项目使用的是 SLF4J 1.6.4。会不会有什么问题?

谢谢

【问题讨论】:

  • 我刚刚发现 ehcache 并没有记录我所期望的所有内容。那么,如果 ehcache 确实记录了您期望的内容,您是否查看过它?

标签: spring ehcache slf4j logback


【解决方案1】:

EhCache 在DEBUG 级别记录了很多。首先这个配置sn -p 过滤掉INFO下面的所有日志语句:

<root level="info">

改成

<root level="ALL">

其次

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

需要提高日志记录级别

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

然后您应该会看到来自 EhCache(和其他)的大量日志记录语句。

【讨论】:

【解决方案2】:

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

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

相反,我建议您设置一个合理的根记录器级别,例如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>

【解决方案3】:

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

【讨论】:

【解决方案4】:

我通过实现 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");
    }
}

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>

【讨论】:

    猜你喜欢
    • 2017-01-14
    • 1970-01-01
    • 2018-07-15
    • 2018-05-06
    • 1970-01-01
    • 1970-01-01
    • 2011-06-16
    • 1970-01-01
    • 2015-08-31
    相关资源
    最近更新 更多