Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use logback configured via logback.groovy with groovy

Tags:

groovy

logback

I am not able to use logback configured via logback.groovy with groovy.

If in a directory i have a script file called for example FooBar.groovy and the file logback.groovy when i run groovy FooBar.groovy groovy tries to compiles the logback configuration file too and my script doesn't work, i have to fallback to the default xml configuration file for logback (logback.xml).

How can i make this to works? can i call groovy somefiles.groovy and configure groovy to ignore logback.groovy?

Thanks for the help

like image 523
res1 Avatar asked Dec 13 '12 19:12

res1


1 Answers

The reason for your problems is that the logback configuration file should never be compiled. It is read at runtime from LogBack via GroovyShell or a similar mechanism.

The solution depends on your project setup. Following you will find a solution for a project build with Gradle following the Maven Standard Directory Layout:

First file is src/main/groovy/Test.groovy:

import org.slf4j.Logger
import org.slf4j.LoggerFactory

class Test {
    static Logger LOG = LoggerFactory.getLogger(Test.class)

    static void main(String[] args) {
        LOG.debug("Test")
    }
}

Second file is src/main/resources/logback.groovy:

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

appender("CONSOLE", ConsoleAppender) {
  encoder(PatternLayoutEncoder) {
    pattern = "%-4relative [%thread] - %msg%n"
  }
}
root(DEBUG, ["CONSOLE"])

I omitted the Gradle build file (build.gradle).

The standard directory layout guarantees that every file in src/main/groovy is compiled, while everything in src/main/resources is included in the class path. So LogBack is able to find that file at runtime.

Update: Didn't read your problem carefully enough. I could not manage to solve the problem when both files are in the same directory and I start it via groovy Test.groovy. I think this is not possible. The groovy command always compiles all groovy files in the current directory and in the given classpath.

like image 194
ChrLipp Avatar answered Oct 06 '22 17:10

ChrLipp