Problem with slf4j logging


(tulsoba) #1

I have written a custom gradle plugin with some custom gradle tasks that I can apply in my other project with:

apply plugin: 'myplugin'

In the tasks I would like to start using slf4j logging so I have done:

import org.gradle.api.DefaultTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
  public class MyTask extends DefaultTask {
  Logger logger = LoggerFactory.getLogger(MyTask.class);
     @TaskAction
  void mytask() {
    logger.error("logback-error");
    logger.warn("logback-warn");
    logger.info("logback-info");

I have the following logback.xml in the root of the myplugin project with the above task:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n</pattern>
    </encoder>
  </appender>
    <root level="DEBUG">
              <appender-ref ref="STDOUT" />
  </root>
      </configuration>

But when I apply the ‘myplugin’ in another project and run the task: 'MyTask ’ it simply prints:

logback-error
:clean
logback-warn

Why are the messages not formatted according to the logback.xml file?


(Peter Niederwieser) #2

The Gradle logging system doesn’t support logback.xml and similar logging configuration files. What you see solely depends on the log level set when running Gradle (or configured in the build script).


(tulsoba) #3

Ok so doing log messages in custom task/plugins needs to be done using system.out?


(Peter Niederwieser) #4

No, just use the appropriate log level. Printing to system out is the same as logging on level ‘org.gradle.api.logging.LogLevel.QUIET’.


(tulsoba) #5

Hm I am a bit confused, The documentation says it uses slf4j:

http://gradle.org/docs/current/userguide/logging.html

and looking into the lib folder of gradle 1.3 it contains logback and slf4j. So has gradle been updated to use slf4j?


(Peter Niederwieser) #6

SLF4J is the API, and logback the implementation.


(tulsoba) #7

Ah misread your post, it was only that the logback.xml was not supported. I am now using :

org.gradle.api.logging.Logger

to get access to the slf4j methods+ “quiet(message)”.


(Peter Niederwieser) #8

If you wish to leverage the Gradle-provided log levels, that’s the way to go.