Change logging level of a task


(adeon) #1

I would like to change logging level of one task in order to display detailed messages in console. According to some gradle-users posts somethings like this should work:

task testLogging {
  logging.level = LogLevel.INFO
    doLast {
        logger.info("Test INFO msg")
  }
}

Unfortunately it doesn’t. The example above doesn’t display the text message on console. What am I doing wrong?


(Peter Niederwieser) #2

As far as I know, it’s a long-standing Gradle bug. I’ve raised GRADLE-2818 for this.


(adeon) #3

I investigated this a little bit more and it seems it only happens when running Gradle tasks from Eclipse (using SpringSource Gradle Integration plugin). From command line it works fine. So it looks like plugin-issue. Sorry I didn’t mention that before, I didn’t expect it may be an Eclipse plug-in fault.


(Peter Niederwieser) #4

I can’t get it to work from command line either (Mac OS 10.8). Last time I investigated this, it was working in our automated tests, but not in the real world, and I couldn’t figure out what the problem is. I tried with several Gradle versions starting from 1.0, to no avail.


(spencer_allain) #5

There are several things that could be in effect here, but the most likely is that logging.level is being set at the configuration phase, so any number of things could be adjusting the logging levels after that task has been parsed.

Try one of the following, depending upon whether you want to see all of the actions that take place after that doFirst block goes into effect, or just after the doLast block takes effect.

task testLogging {
  doFirst {
    logging.level = LogLevel.INFO
  }
  doLast {
    logger.info("Test INFO msg")
  }
}
task testLogging {
  doLast {
    logging.level = LogLevel.INFO
    logger.info("Test INFO msg")
  }
}

Unfortunately, there currently isn’t any public way to revert the logging back to whatever it was, because almost all of the time the logging level is inherited, so it returns null, and attempting to set the current logging level to null results in a NullPointerException in at least the o.g.l.i.logback.LogLevelConverter(). Maybe a LoggingLevel.NULL or LoggingLevel.INHERITED, should be added to the gradle LoggingLevel, or it should just handle null to mean put logging back into the inherited mode (if not the root logger).

-Spencer