res1
res1

Reputation: 3640

How to use logback configured via logback.groovy

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?

Upvotes: 10

Views: 13465

Answers (2)

Andreas Covidiot
Andreas Covidiot

Reputation: 4745

I had the same problem in an Eclipse Greclipse environment (and came to this question as best problem reference via Google). It will be easily adaptable to other Groovy environments as well and the basic principles/setup will hold true.

Here is what needs to be done/ensured:

Project -> Properties -> Java Build Path ...

  • -> Source -> Add Folder or check properly already set up
    • all relevant files must be in the Included (e.g. (All)) and not in the Excluded (e.g. (None)) patterns
    • src/main/groovy/ (should contain your Groovy classes)
    • src/test/resources/ (where logback.groovy is directly underneath)
  • make sure that under -> Output Folders the src/main/resources is before src/test/resources so the right logback.groovy (if you have one for test as well) is taken in the normal run

Project -> Properties -> Groovy Compiler -> Groovy Script Folders ...

  • [ ] src/main/groovy/**/*.groovy (or similar pattern for your classes must be unchecked)
  • [x] src/main/resources/**/*.groovy (or similar pattern for your normal logback.groovy must be checked)
  • [x] src/test/resources/**/*.groovy (or similar pattern for your test logback.groovy must be checked)

test and normal logback.groovy should work this way.

As a result you should see the following copied from the source resource to the target folders:

  • target/classes/logback.groovy
  • target/test-classes/logback.groovy

side note: Furthermore, if you are using Groovy inside ANT in Eclipse, all you have to do is Windows -> Preferences -> Ant -> Runtime -> Classpath -> Ant Home Entries -> Add Variable... -> ${project_classpath} and logback.groovy will also be applied properly :). (Since the ${project_classpath} does not seem to (always) export in the above order, we had to manually Add Folder -> target/classes and move above the other to make it work with the right logback.groovy)

Upvotes: 0

ChrLipp
ChrLipp

Reputation: 15668

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.

Upvotes: 14

Related Questions