13

我想使用 Groovy DSL 配置 Logback。该文件非常简单:

import ch.qos.logback.classic.encoder.PatternLayoutEncoder
import ch.qos.logback.core.ConsoleAppender
import static ch.qos.logback.classic.Level.DEBUG
import static ch.qos.logback.classic.Level.INFO

appender("stdout", ConsoleAppender) {
    encoder(PatternLayoutEncoder) {
        pattern = "%d %p [%c] - <%m>%n"
    }
}

root(INFO, ["stdout"])

我使用 Gradle 构建我的应用程序并使用 jettyRun 运行它。我收到以下错误:

Failed to instantiate [ch.qos.logback.classic.LoggerContext]
Reported exception:
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'ch.qos.logback.core.ConsoleAppender[null]' with class 'ch.qos.logback.core.ConsoleAppender' to class 'ch.qos.logback.core.Appender'
    at  org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:360)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.castToType(ScriptBytecodeAdapter.java:599)
at ch.qos.logback.classic.gaffer.ConfigurationDelegate.appender(ConfigurationDelegate.groovy:119)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
at org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod.invoke(MixinInstanceMetaMethod.java:53)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoMetaMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:308)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:52)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:46)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:133)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:149)

但是,当我切换到等效的 XML 配置时,一切正常。我究竟做错了什么?

使用 Logback 1.0.0。尝试使用 Logback 1.0.3。

4

3 回答 3

14

我想出了解决方案,但仍有一些问题悬而未决。问题是我在类路径上没有合适的 Groovy。我决定制作一个示例项目来演示这个错误。我从使用 Gradle 的“应用程序”插件的控制台应用程序开始。我没有将 Groovy 作为依赖项。

apply plugin: 'application'

repositories {
    mavenCentral()
}

ext.logbackVersion = '1.0.3'
ext.slf4jVersion = '1.6.4'

dependencies {
    compile "ch.qos.logback:logback-classic:$ext.logbackVersion"
    compile "org.slf4j:jcl-over-slf4j:$ext.slf4jVersion"
    //runtime "org.codehaus.groovy:groovy:1.8.6" // the problem was here
}

mainClassName = "org.test.Main"

这给了我一个错误,这是非常直截了当的。

|-ERROR in ch.qos.logback.classic.LoggerContext[default] - Groovy classes are not available on the class path. ABORTING INITIALIZATION.

嗯不错。缺少依赖项 - 易于修复。但是为什么我在运行 Web 应用程序时没有收到同样的错误?添加 Groovy 依赖项解决了 Web 应用程序中的初始问题。我剥离了我的项目并将创建一个相应的 JIRA。也许,Groovy 在类路径检测上并不完全正确。

于 2012-06-13T12:51:42.440 回答
1

The GroovyCastException "cannot cast ConsoleAppender as Appender" has all the bearings of a class loader issue. Which version of groovy is this? Could you open a bug report including a test case for reproducing this issuew?

于 2012-06-12T14:04:52.957 回答
0

同事。

我今天遇到了几乎同样的麻烦:

  • 当我使用logback.xml一切正常
  • 当我logback.groovy在 IntelliJ IDEA 中使用时,一切都可以正常工作
  • logback.groovy当我从命令行启动脚本时使用时,我遇到了很多错误,例如

D:\Projects\PRDMonitoring\sources>groovy tray.groovy PRD
Failed to instantiate [ch.qos.logback.classic.LoggerContext]
Reported exception:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1.groovy: 2: unable to resolve class ch.qos.logback.classic.filter.LevelFilter
@ line 2, column 1.
   import ch.qos.logback.classic.filter.LevelFilter
   ^
Script1.groovy: -1: unable to resolve class ch.qos.logback.classic.encoder.PatternLayoutEncoder
 @ line -1, column -1.
Script1.groovy: 3: unable to resolve class ch.qos.logback.core.ConsoleAppender
 @ line 3, column 1.
   import ch.qos.logback.core.ConsoleAppender
   ^
Script1.groovy: -1: unable to resolve class ch.qos.logback.classic.Level
 @ line -1, column -1.
Script1.groovy: 6: unable to resolve class ch.qos.logback.core.spi.FilterReply
 @ line 6, column 1.
   import static ch.qos.logback.core.spi.FilterReply.ACCEPT
   ^
Script1.groovy: 7: unable to resolve class ch.qos.logback.core.spi.FilterReply
 @ line 7, column 1.
   import static ch.qos.logback.core.spi.FilterReply.DENY

但是几分钟后找到一个解决方案,我发现,注释 @Grapes的以下字符串解决了加载类的问题@GrabConfig(systemClassLoader=true)

@GrabConfig(systemClassLoader=true)
@Grapes([
    @Grab(group = 'org.codehaus.groovy.modules.http-builder', module = 'http-builder', version = '0.6'),
    @Grab(group = 'org.apache.commons', module='commons-lang3', version='3.0'),
    @Grab(group = 'commons-io', module = 'commons-io', version = '2.4'),
    @Grab(group = 'joda-time', module = 'joda-time', version = '2.9.4'),
    @Grab(group = 'ch.qos.logback', module = 'logback-classic', version = '1.1.7'),
    @Grab(group = 'ch.qos.logback', module = 'logback-core', version = '1.1.7')
])
于 2016-09-03T18:36:49.977 回答