0

I am trying to get logging enabled in my Grovvy unit tests that I am running with Cucumber-jvm.

In my tests I have imported:

import groovy.util.logging.Slf4j

Then added

@Slf4j

However, when I run any of these commands:

mvn test -Dgmaven.logging=DEBUG
mvn test -Dgmaven.logging=TRACE

This method only prints out '[WARN] log.isWarnEnabled()'

void debugLogging(){
    println("~~~~~ debugLogging() ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    if(log.isTraceEnabled()){
        println("log.isTraceEnabled()")
        log.trace("log.isTraceEnabled()")
    }
    if(log.isDebugEnabled()){
        println("log.isDebugEnabled()")
        log.debug("log.isDebugEnabled()")
    }
    if(log.isInfoEnabled()){
        println("log.isInfoEnabled()")
        log.info("log.isInfoEnabled()")
    }
    if(log.isWarnEnabled()){
        println("log.isWarnEnabled()")
        log.warn("log.isWarnEnabled()")
    }
}

Here is my ./src/test/resources/logback.xml :

<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>
                %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
            </pattern>
        </encoder>
    </appender>

    <!-- Use level="DEBUG" for more detailed logging -->
    <root level="debug">
        <appender-ref ref="STDOUT" />
    </root>

</configuration>

How can I change the log level in my unit test?

Mick Knutson
  • 2,297
  • 3
  • 25
  • 48

1 Answers1

1

I discovered the hard way that GMaven does not use logback. The plugin is designed to leverage Sonatype's default SLF4J provided, called gossip (Described in the GMaven advanced configuration)

So this might explain why your logback.xml file was ignored (Although one would think SLF4J calls would be redirected to the configured runtime provider when run from unit tests)

To pass the gmaven.logging property into you're unit tests you could try setting it within the surefire plugin as follows:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.12.4</version>
    <configuration>
      <systemPropertyVariables>
        <gmaven.logging>DEBUG</gmaven.logging>
      </systemPropertyVariables>
    </configuration>
  </plugin>

Hope this helps.

Update

I came across this problem when developing a Maven plugin. My challenge was to capture logging calls originating in a library that did not using SLF4J....

To solve this problem, I added one of the bridging APIs to my plugin dependencies section as follows:

  <plugin>
    ..
    ..
    <dependencies>
      <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>1.5.10</version>
      </dependency>
    </dependencies>
  </plugin>

This will re-route commons logging calls over to your SLF4J implementation, which in the case of Maven will be gossip.

Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185
  • I was already putting '-Dgmaven.logging=DEBUG' to the command-line. But I did add this to surefire just in case, and that did not change anything. Still only [WARN] logs are being printed. all others are ignored. – Mick Knutson Nov 15 '12 at 13:36
  • @MickKnutson What a pain.... What does your runtime classpath look like? Any chance these logging calls are being intercepted by another logging provider like log4j? (Often included as a transitive dependency) – Mark O'Connor Nov 15 '12 at 17:10
  • Does anyone have a working .gossip/config.properties that will work with gossip? I cannot get it configured. – Mick Knutson Jan 15 '13 at 02:12
  • @MickKnutson In my case I just went with the default settings. Setting the gmaven.logging property allowed me to set the log level. In my case I was developing a plugin (rather than attempting to run unit tests). I've included details of what I did. Hope this helps. – Mark O'Connor Jan 15 '13 at 17:04