Unable to use logback.groovy, but logback.xml works
Asked Answered
M

3

13

I wanted to configure Logback using Groovy DSL. The file is very simple:

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"])

I use Gradle to build my application and run it with jettyRun. I get the following error:

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)

However, when I switch to equivalent XML configuration, everything works. What am I doing wrong?

Using Logback 1.0.0. Tried with Logback 1.0.3.

Moats answered 12/6, 2012 at 11:6 Comment(0)
M
14

I figured the solution out, but some questions remain opened. The problem was that I had no proper Groovy on the classpath. I decided to make an example project to demonstrate this bug. I started with a console application using Gradle's "application" plugin. I didn't include Groovy as dependency.

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"

This gave me an error, which is quite straighforward.

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

OK, cool. The dependency was missing - easy to fix. But why didn't I get the same error when I ran my web application? Adding Groovy dependency solved the initial problem in the web application. I stripped my project down and will create a corresponding JIRA. Perhaps, Groovy on classpath detection is not quite correct.

Moats answered 13/6, 2012 at 12:51 Comment(0)
L
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?

Listless answered 12/6, 2012 at 14:4 Comment(2)
OK, I'll create a sample project. I use Groovy 1.8.6.Moats
Great. Looking forward to the bug report.Listless
D
0

colleagues.

I have faced almost the same trouble today:

  • When I use logback.xml everything works fine
  • When I use logback.groovy in IntelliJ IDEA eveything works fine too
  • When I use logback.groovy when start my script from command line I got a lot of errors like

:

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

But after a couple of minutes to find a solution I figured out, that the following string before @Grapes annotation fixes a trouble with classes loading @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')
])
Delorsedelos answered 3/9, 2016 at 18:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.