2012-06-12 60 views
11

我想使用Groovy DSL配置Logback。該文件非常簡單:無法使用logback.groovy,但logback.xml工作

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

GroovyCastException「無法將ConsoleAppender轉換爲Appender」具有類加載器問題的所有方面。這是什麼版本的groovy?你能否打開一個bug report包括一個測試用例來重現這個問題?

+0

好的,我將創建一個示例項目。我使用Groovy 1.8.6。 – Infeligo

+0

太好了。期待錯誤報告。 – Ceki

9

我想出瞭解決方案,但仍然有一些問題打開。問題在於我沒有在類路徑中使用適當的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不太正確。

0

同事。

我面臨幾乎同樣的問題今天:

  • 當我使用logback.xml一切正常
  • 當我在的IntelliJ IDEA當我使用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') 
]) 
麻煩
相關問題