spring 如何启用 Ehcache 的日志记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10705107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to enable logging of Ehcache
提问by Tamim
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.
在我的 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>
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?
Ehcache 使用的是 SLF4J 1.6.1,而我的项目使用的是 SLF4J 1.6.4。它会导致任何问题吗?
Thanks
谢谢
回答by Tomasz Nurkiewicz
EhCache logs a lot on DEBUGlevel. First of all, this configuration snippet filters out all logging statements below INFO:
EhCache 在DEBUG级别上记录了很多。首先,这个配置片段过滤掉下面的所有日志语句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).
然后,您应该会看到大量来自 EhCache(和其他)的日志记录语句。
回答by Nabil_H
In my opinion setting the root logger level to ALLis not a good idea.
在我看来,将根记录器级别设置ALL为不是一个好主意。
This means that ALLlog messages of every level will get through (unless you set a threshold on your appenders).
这意味着ALL每个级别的日志消息都会通过(除非您在附加程序上设置阈值)。
Instead, I suggest you set a sensible root logger level, such as INFOor WARN, and then set the log level of individual loggers if you require more specific information.
相反,我建议您设置一个合理的根记录器级别,例如INFO或WARN,然后如果您需要更具体的信息,然后设置单个记录器的日志级别。
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>
回答by ben3000
I also found it handy to enable DEBUG logging on org.springframework.cachebecause I'm using Spring Framework 4.2.1's caching feature (@Cacheable etc.).
我还发现启用 DEBUG 登录很方便,org.springframework.cache因为我使用的是 Spring Framework 4.2.1 的缓存功能(@Cacheable 等)。
回答by Hany Sakr
I wrote a custom logger for Ehcache 3 by implementing the CacheEventListener interface as follows
我通过实现 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");
}
}
The ehcache.xml will define the listener class
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>

