ChrLipp
ChrLipp

Reputation: 15668

Logback configuration in Groovy and different class paths in Gradle/Idea

I am using SLF4J and Logback for logging in a Groovy application, therefore I also configure logback via Groovy configuration. My config is very easy and looks like:

import static ch.qos.logback.classic.Level.INFO
import static ch.qos.logback.classic.Level.DEBUG

import ch.qos.logback.classic.encoder.PatternLayoutEncoder
import ch.qos.logback.core.ConsoleAppender
import ch.qos.logback.core.status.OnConsoleStatusListener

// always a good idea to add an on console status listener
statusListener(OnConsoleStatusListener)

// setting up appenders
appender('CONSOLE', ConsoleAppender) {
    encoder(PatternLayoutEncoder) {
        pattern = "%d [%thread] %-5level %logger - %msg%n"
    }
}

// setting up loggers
logger("org.apache", INFO)
root(DEBUG, ['CONSOLE'])

However, I came into the problem that org.apache logging messages also occur on debug level which shouldn't be according to the config above (I also tried to change the order of looger and root). I found out that the config isn't loaded at all when running the app from IDEA. But when I start it via command line (just executing gradlew run) everything works.

Question 1: Does anyone know what happens here? It seems that the class path started from IDEA is different than the class path started from gradlew and that there is another logback config file available. But the classpath in IDEA is only java and the gradle dependencies. In my opionion (just read it from here) logback should look for a logback.groovy at first and I am quite sure that my file is the only one.

Then I tried to translage a working XML config to Groovy via the online translator. The config is just the same but I noticed that the logback debug option gets lost:

<configuration debug="true"> 
    ...
</configuration> 

Question 2: Does anyone know how to enable logback debug via Groovy? (debug = true is NOT working)

The full Gradle build file:

// Plugins
apply plugin: 'groovy'
apply plugin: 'application'
apply plugin: 'idea'

// Dependencies
configure(allprojects)
{
    ext.groovy = '2.1.0'
    ext.slf4jVersion = '1.7.2'
    ext.logbackVersion = '1.0.9'
    ext.apacheFluentHc = '4.2.3'
}

repositories {
    mavenCentral()
}

configurations {
    compile.exclude module: 'commons-logging'
}

dependencies {
    // Groovy
    compile "org.codehaus.groovy:groovy-all:${groovy}:indy"

    // Logging
    compile "org.slf4j:slf4j-api:$slf4jVersion"
    compile "ch.qos.logback:logback-classic:$logbackVersion"
    compile "org.slf4j:jcl-over-slf4j:$slf4jVersion"

    // Apache HttpClient
    compile "org.apache.httpcomponents:fluent-hc:$apacheFluentHc"
}

// Java options
sourceCompatibility = 1.7
targetCompatibility = 1.7
mainClassName = 'XXX'

// Groovy options
[compileGroovy.groovyOptions, compileTestGroovy.groovyOptions]*.with {
    fork = true
    optimizationOptions = [ indy: true, 'int': false]
    encoding = 'UTF-8'
}

// Tasks
task wrap(type:Wrapper, description:"create a gradlew") {
    gradleVersion = '1.4'   
}

Upvotes: 2

Views: 4636

Answers (1)

ChrLipp
ChrLipp

Reputation: 15668

I solved question 1, logback.groovy just wasn't found and therefore a default config was loaded.

When I executed the application in IDEA, I did just run the mainclass and not gradle run. I am used to this from Eclipse, where the output path from Maven and Eclipse are ident. But this is not the case with IDEA and Gradle. While the output path for IDEA is out\production\<project>, it is build\classes\main for Gradle and IDEA does not copy the resources to it's output path.

Now I use gradle run within IDEA and therefore bypass the IDEA build.

Question 2 is just not implemented (Logback 1.0.11), there is no DSL for debug, see code.

Upvotes: 3

Related Questions