0

I can't make Gradle show (highlighted) warnings that I emit from my build script using

logger.warn("something something")

The line is emitted plain, like any other log line, it does not have a WARN next to it, I was expecting some colour with --console=rich, nothing.

Also, the warning is simply emitted at the respective place in the log. I would like them emitted there but also at the end, or at least something at the end to indicate there were warnings. Supposedly --warning-mode=summary is for this but doesn't have any effect.

I tried all possible combinations like:

gradle --warning-mode=summary build
gradle build --warning-mode=all
gradle build --warning-mode all
gradle build -Dorg.gradle.warning.mode=all --console=rich --info
etc

I tried gradle 5.2 and 5.4.1. No warnings!

Daniele
  • 2,672
  • 1
  • 14
  • 20
haelix
  • 4,245
  • 4
  • 34
  • 56

1 Answers1

2

warn messages from the logger always look like that. They are visible by default; while info messages are only shown when using --info flag (or something more verbose).

Actually the logger is based on slf4j; but then it is up to Gradle to set the logging format (and they chose not to show the log level, nor the timestamp etc.)

When calling logger.warn(..), that resolves to project.logger. See the reference for Project.getLogger() , and for the Logger .


One way to show colored output (see this SO post) is by using Gradle internal API like

import org.gradle.internal.logging.text.StyledTextOutput;
import org.gradle.internal.logging.text.StyledTextOutputFactory;
def out = services.get(StyledTextOutputFactory).create("my-factory")
out.withStyle(StyledTextOutput.Style.FailureHeader).println('Failure message')
out.withStyle(StyledTextOutput.Style.Success).println('Success message')

A dirty (and trivial) way to show "WARN" in your messages is obviously to use logger.warn('WARN: message').


Finally, instead of printing warnings, tasks can be interrupted by throwing exceptions, so that Gradle will stop the build and show what went wrong, eg. when running gradle taskA with:

task taskB {
  doLast{
    throw new Exception("error here")
  }
}

task taskA {
  dependsOn taskB
}
Daniele
  • 2,672
  • 1
  • 14
  • 20
  • OK. Exceptions is what I moved away from, because I have a non-fatal use-case but that is really important to highlight to the user. Given that `StyledTextOutput` is internal API (and old posts too, and I don't see a preset for warnings), I will rest with the "dirty and trivial" solution that I had already implement – haelix Jun 15 '19 at 20:07
  • But the question is not so much about the colours, what's more important is the ability to show a summary at the end. Why doesn't **--warning-mode=summary** work? Gradle seems to be very good at outputting `Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0` warning, it should somehow facilitate user warnings also? – haelix Jun 15 '19 at 20:09