我想使用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。
答案 0 :(得分:10)
我想出了解决方案,但有些问题仍未解决。问题是我在类路径上没有正确的Groovy。我决定制作一个示例项目来演示这个bug。我使用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不太正确。
答案 1 :(得分:0)
GroovyCastException“无法将ConsoleAppender强制转换为Appender”具有类加载器问题的所有方面。这是哪个版本的groovy?你能打开一个bug report,包括一个用于复制这个问题的测试用例吗?
答案 2 :(得分:0)
的同事。
我今天遇到了几乎同样的麻烦:
logback.xml
时,一切正常logback.groovy
时,eveything也可以正常工作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 =真) 强>
@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')
])