如何启用Ehcache的日志记录

16
在我的Spring + Hibernate项目中,我使用SLF4J 1.6.4和LogBack进行日志记录。现在,我通过ehcache-spring-annotations-1.1.3添加了Ehcache 2.2.0。缓存似乎正在工作,因为方法(使用@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以确定它是否记录了你期望的内容? - codesmith
4个回答

54

EhCache在DEBUG级别下记录了很多日志。首先,此配置片段过滤掉所有低于INFO的日志语句:

<root level="info">

将其更改为

<root level="ALL">

其次

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

需要增加日志级别

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

然后你应该能够看到来自EhCache(和其他组件)的大量日志记录语句。


有人可以看一下我发布的问题吗:https://dev59.com/G57ha4cB1Zd3GeqPh1ie - user18853

14

我认为将根记录器级别设置为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>

:这是一个空的段落标签,用于在HTML中创建一个新的段落。

请参考http://www.ehcache.org/documentation/2.8/operations/logging.html#recommended-logging-levels。 - Lucky

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");
    }
}

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>

这个解决方案对我来说很有效,使用了ehcache3。 - ajaristi
2.x怎么样?我尝试过了,但是好像缺少CacheEvent类,所以无法正常工作。+1 - samshers
监听器只接受5个事件。当您想要在ehcache被“调用”时记录日志时怎么办? - FrankelStein

5

我发现在使用Spring Framework 4.2.1的缓存功能(@Cacheable等)时,启用org.springframework.cache的DEBUG日志记录非常方便。


这是指将以下行添加到 application.properties 文件中吗: "logging.level.org.springframework.cache: DEBUG" - user18853
是的,那样做可以,但也有其他方法可以实现 - ben3000

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接