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?
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.