java - 如何在使用 Hibernate 时删除 Ehcache WARN 消息

标签 java hibernate logging configuration ehcache

我有这个 Ehcache XML 配置:

<ehcache>
    <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="false"
            diskSpoolBufferSizeMB="30"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU"
            />
</ehcache>

此外,我还有一些包含实体的包(大约 150 个)。如果我在 tomcat 服务器上部署我的应用程序,日志中有很多 WARN 消息:

2015-04-29 11:59:02,712 [RMI TCP Connection(3)-127.0.0.1] WARN org.hibernate.cache.ehcache.AbstractEhcacheRegionFactory - HHH020003: Could not find a specific ehcache configuration for cache named [com.company.project.entity.package.MyEntity]; using defaults.

我可以为每个实体编写配置 -

<cache name="com.company.project.entity.package.MyEntity"
                      maxEntriesLocalHeap="50"
                      eternal="false"
                      overflowToDisk="false"
                    timeToLiveSeconds="120">
                <persistence strategy="localTempSwap"/>
        </cache>

但是这样一来,我的配置文件就变得太大了(1600 行)。我认为还有另一种方法可以为每个实体设置默认配置并终止警告消息,但我不知道该怎么做。如果有人知道,请帮助。非常感谢。

最佳答案

这是记录警告消息的 Hibernate EHCache 缓存区域代码:

private Ehcache getCache(String name) throws CacheException {
    try {
        Ehcache cache = manager.getEhcache( name );
        if ( cache == null ) {
            LOG.unableToFindEhCacheConfiguration( name );
            manager.addCache( name );
            cache = manager.getEhcache( name );
            LOG.debug( "started EHCache region: " + name );
        }
        HibernateEhcacheUtils.validateEhcache( cache );
        return cache;
    }
    catch (net.sf.ehcache.CacheException e) {
        throw new CacheException( e );
    }

}

如您所见,您有两个选择:

  1. 您可以在 ehcache.xml 文件中声明缓存区域。
  2. 您将“org.hibernate.cache.ehcache.AbstractEhcacheRegionFactory”日志级别设置为错误:

    对于 Log4j2:

    <Logger name="org.hibernate.cache.ehcache.AbstractEhcacheRegionFactory" level="error">
        <AppenderRef ref="File"/>
    </Logger>
    

关于java - 如何在使用 Hibernate 时删除 Ehcache WARN 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29947671/

相关文章:

java - TicTacToe 数组越界

java - 通过嵌套列表对象中的多个值查找对象 Hibernate、Jpa

logging - Loki 日志中各路径的平均响应时间

java - 执行通用 Java 类

java - @Cacheable 通过 Spring aop - 如何生成唯一的缓存键

java - SecurityContextLogoutHandler 清除身份验证,即使设置为 'false'

java - 使用 hibernate 将金额应用于不同的行

mysql - org.apache.tomcat.dbcp.dbcp.SQLNestedException : Cannot create JDBC driver of class '' for connect URL 'null'

python - 仅将信息级别的条目记录到单独的文件中

python - 服务器日志记录 - 在数据库或日志文件中?