Grails ClassNotFoundException:org.apache.log4j.Logger

时间:2014-11-21 11:35:24

标签: grails logging log4j logback

我正在尝试使用logback plugin让我的Grails应用程序记录。根据文档的说明,这是我到目前为止所做的:

BuildConfig
===========
grails.project.dependency.resolution = {
    inherits("global") {
        excludes 'grails-plugin-log4j', 'log4j', 'h2'
    }

    log "error"
    checksums true
    legacyResolve false

    repositories {
        inherits true

        grailsPlugins()
        grailsHome()
        mavenLocal()
        grailsCentral()
        mavenCentral()

        // For logback plugin.
        mavenRepo "http://repo.grails.org/grails/libs-releases/"
    }

    dependencies {
        // This plugin is a binary plugin, so it belongs here instead of under 'plugins'.
        compile 'org.grails.plugins:logback:0.3.1'

        test "org.grails:grails-datastore-test-support:1.0-grails-2.4"
    }
}

Config
======
import ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy
import ch.qos.logback.core.rolling.FixedWindowRollingPolicy

<lots of more configs here>

logback = {
    appenders {
    rollingFile name:
        'rollingAppender',
        file: '/tmp/rolling.log',
        encoder: pattern(pattern: '%-4relative [%thread] %-5level %logger{35} - %msg%n'),
        triggeringPolicy: new SizeBasedTriggeringPolicy(maxFileSize: 10*1024*1024),
        rollingPolicy: new FixedWindowRollingPolicy(fileNamePattern: '/tmp/rolling.%i.log.gz')
    }

    root {
        info rollingAppender
    }

    error rollingAppender: 'org.codehaus.groovy.grails.web.servlet',
        'org.codehaus.groovy.grails.web.pages'
}

然后我跑了grails clean。当我执行grails run-app时,我得到一个绝对大量的堆栈跟踪,如下所示:

|Compiling 161 source files
    [groovyc] org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
    [groovyc] General error during semantic analysis: Unable to load logging class
    [groovyc] 
    [groovyc] groovy.lang.GroovyRuntimeException: Unable to load logging class
    [groovyc]   at org.codehaus.groovy.transform.LogASTTransformation$AbstractLoggingStrategy.classNode(LogASTTransformation.java:269)
    [groovyc]   at groovy.util.logging.Log4j$Log4jLoggingStrategy.addLoggerFieldToClass(Log4j.java:72)
    [groovyc]   at org.codehaus.groovy.transform.LogASTTransformation$1.visitClass(LogASTTransformation.java:112)
    [groovyc]   at org.codehaus.groovy.transform.LogASTTransformation.visit(LogASTTransformation.java:157)

<massive stack trace omitted for brevity>

General error during semantic analysis: Unable to load logging class
groovy.lang.GroovyRuntimeException: Unable to load logging class
    at org.codehaus.groovy.transform.LogASTTransformation$AbstractLoggingStrategy.classNode(LogASTTransformation.java:269)
    at groovy.util.logging.Log4j$Log4jLoggingStrategy.addLoggerFieldToClass(Log4j.java:72)
    at org.codehaus.groovy.transform.LogASTTransformation$1.visitClass(LogASTTransformation.java:112)

<massive stack trace omitted for brevity>

    at org.codehaus.groovy.grails.cli.support.GrailsStarter.rootLoader(GrailsStarter.java:236)
    at org.codehaus.groovy.grails.cli.support.GrailsStarter.main(GrailsStarter.java:264)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:655)
    at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:765)
    at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:753)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:270)
    at org.codehaus.groovy.transform.LogASTTransformation$AbstractLoggingStrategy.classNode(LogASTTransformation.java:267)
... 608 more
6 errors

此外,在我的GGTS IDE中,我在添加到Config.groovy的两个import语句中遇到编译器错误。它听起来就像我的Grails应用程序中仍然有Log4j引用一样,并且运行grails clean从我的类路径中删除了Log4j JAR。这可能解释了堆栈跟踪。但它并没有解释为什么Grails(或者至少是我的GGTS IDE)找不到我正在导入的logback类。

我确实在我的Config

中看到了这一点
grails.logging.jul.usebridge = true

所以我问:如何修复此堆栈跟踪和IDE错误?


更新

执行grails dependency-report会产生一个巨大的图表,似乎显示我在log4j上还有2个依赖项:

+--- org.grails.plugins:tomcat:7.0.54
|    \--- org.apache.tomcat:tomcat-catalina-ant:7.0.53
|    \--- org.apache.tomcat.embed:tomcat-embed-jasper:7.0.53
|         \--- org.apache.tomcat.embed:tomcat-embed-el:7.0.53

<HERE>
|    \--- org.apache.tomcat.embed:tomcat-embed-logging-log4j:7.0.53

+--- org.grails:grails-plugin-rest:2.4.2
|    \--- org.slf4j:jcl-over-slf4j:1.7.5
|    \--- org.slf4j:slf4j-api:1.7.5
|    \--- com.google.code.gson:gson:2.2.4
|    \--- org.grails:grails-web:2.4.2
|         \--- org.grails:grails-web-common:2.4.2
|              \--- org.springframework:spring-webmvc:4.0.5.RELEASE
|              \--- org.springframework:spring-context-support:4.0.5.RELEASE
|              \--- org.grails:grails-databinding:2.4.2
|         \--- org.grails:grails-web-url-mappings:2.4.2
|         \--- org.grails:grails-web-fileupload:2.4.2
|              \--- commons-fileupload:commons-fileupload:1.3.1
|                   \--- commons-io:commons-io:2.2
|         \--- org.grails:grails-web-gsp:2.4.2
|         \--- org.springframework:spring-aspects:4.0.5.RELEASE
|         \--- org.grails:grails-web-mvc:2.4.2
|         \--- org.grails:grails-web-sitemesh:2.4.2
|              \--- opensymphony:sitemesh:2.4
|         \--- org.grails:grails-web-databinding:2.4.2
|         \--- org.grails:grails-web-jsp:2.4.2
|         \--- org.aspectj:aspectjweaver:1.8.0
|         \--- org.aspectj:aspectjrt:1.8.0
|    \--- org.grails:grails-plugin-controllers:2.4.2
|         \--- org.grails:grails-plugin-validation:2.4.2
|    \--- org.grails:grails-plugin-datasource:2.4.2
|         \--- org.springframework:spring-jdbc:4.0.5.RELEASE

<HERE>
|         \--- org.apache.tomcat.embed:tomcat-embed-logging-log4j:7.0.50

所以:

  • org.grails.plugins:tomcat:7.0.54取决于:
    • org.apache.tomcat.embed:tomcat-embed-logging-log4j:7.0.53;和
  • org.grails:grails-plugin-rest:2.4.2取决于:
    • org.grails:grails-plugin-datasource:2.4.2取决于:
    • org.apache.tomcat.embed:tomcat-embed-logging-log4j:7.0.50

所以我的下一个问题:这些是罪魁祸首吗?如果是这样,我该如何安全地删除它们?

1 个答案:

答案 0 :(得分:2)

我有完全相同的堆栈跟踪,并通过添加一个slf4j log4j桥来解决它,以确保仍依赖于log4j的所有传递或插件现在将记录回溯:

runtime "org.slf4j:jul-to-slf4j:1.7.7"
runtime "org.slf4j:log4j-over-slf4j:1.7.7"

希望有所帮助。