Grails 2.x记录器冲突(slf4j,log4j和commons-logging)

时间:2012-07-17 14:43:06

标签: grails log4j slf4j

将现有应用程序从Grails 1.3.7升级到Grails 2.0.4(也尝试过2.0.3)后,我遇到了某种slf4jlog4j冲突。似乎

  • log4j尝试使用commons-logging
  • 写入
  • commons-logging写入slf4j
  • 但是slf4j logger appender使用log4j

循环参考。结果java.lang.StackOverflowError

java.lang.StackOverflowError
    at org.apache.log4j.PatternLayout.format(PatternLayout.java:500)
    at org.apache.log4j.AppenderSkeleton.doAppend(AppenderSkeleton.java:251)
    at org.apache.log4j.helpers.AppenderAttachableImpl.appendLoopOnAppenders(AppenderAttachableImpl.java:66)
    at org.apache.log4j.Category.callAppenders(Category.java:206)
    at org.apache.log4j.Category.forcedLog(Category.java:391)
    at org.apache.log4j.Category.log(Category.java:856)
    at org.slf4j.impl.GrailsLog4jLoggerAdapter.logMessage(GrailsLog4jLoggerAdapter.java:191)
    at org.slf4j.impl.GrailsLog4jLoggerAdapter.error(GrailsLog4jLoggerAdapter.java:166)
    at org.apache.commons.logging.impl.SLF4JLog.error(SLF4JLog.java:213)
    at org.apache.log4j.AppenderSkeleton.doAppend(AppenderSkeleton.java:251)
    at org.apache.log4j.helpers.AppenderAttachableImpl.appendLoopOnAppenders(AppenderAttachableImpl.java:66)
    at org.apache.log4j.Category.callAppenders(Category.java:206)
    at org.apache.log4j.Category.forcedLog(Category.java:391)
    at org.apache.log4j.Category.log(Category.java:856)
    at org.slf4j.impl.GrailsLog4jLoggerAdapter.logMessage(GrailsLog4jLoggerAdapter.java:191)
    at org.slf4j.impl.GrailsLog4jLoggerAdapter.error(GrailsLog4jLoggerAdapter.java:166)
    at org.apache.commons.logging.impl.SLF4JLog.error(SLF4JLog.java:213)
    at org.apache.log4j.AppenderSkeleton.doAppend(AppenderSkeleton.java:251)
    at org.apache.log4j.helpers.AppenderAttachableImpl.appendLoopOnAppenders(AppenderAttachableImpl.java:66)
    at org.apache.log4j.Category.callAppenders(Category.java:206)
    at org.apache.log4j.Category.forcedLog(Category.java:391)
    at org.apache.log4j.Category.log(Category.java:856)
    at org.slf4j.impl.GrailsLog4jLoggerAdapter.logMessage(GrailsLog4jLoggerAdapter.java:191)
    at org.slf4j.impl.GrailsLog4jLoggerAdapter.error(GrailsLog4jLoggerAdapter.java:166)
    ...

似乎log4j记录到commons-logging,commons-loggin

我尝试从依赖项中排除log4j,并使用log4j-over-slf4j作为重播,但是一些Grails类具有直接的Log4J用法:

java.lang.NoClassDefFoundError: org/apache/log4j/LogManager
    at org.slf4j.impl.GrailsSlf4jLoggerFactory.getLogger(GrailsSlf4jLoggerFactory.java:43)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:253)
    at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogFactory.java:156)
    at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogFactory.java:132)
    at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:272)

我不能排除slf4j,因为它被标准的Grails插件使用,比如Resources插件。

Btw,我以前从未遇到过其他Grails 2.0.3项目。似乎它只是针对当前应用程序的配置或依赖项,但我找不到任何异常。

有人有同样的问题吗?

2 个答案:

答案 0 :(得分:3)

通过取消注释默认appender修复:

appenders {
    console name:'stdout', layout:pattern(conversionPattern: '%c{2} %m%n')
}

现在工作,但这很奇怪

答案 1 :(得分:0)

我有同样的StackOverflowError。它发生在Grails尝试在我的应用程序启动期间记录一个完全不同的异常时。所以这个问题与Grails记录异常有关。

我通过在我的log4j配置中注释这行来修复它(在Config.groovy中):

error stdout: "StackTrace"

之后StackOverflowError消失了,我可以看到潜在的异常。