Gradle Plugin Logging without the Gradle API

It certainly seems to be good practice to separate Gradle Plugin code into code and tests which do not depend on any Gradle API and Gradle Plugin specific code , see also https://github.com/gradle-guides/testing-gradle-plugins/tree/master/samples/code/url-verifier-plugin

Logging for the Gradle API dependent code is straight forward and well supported.

For me it’s not quite clear, how to do logging in the Gradle independent code, like for example in https://github.com/gradle-guides/testing-gradle-plugins/blob/master/samples/code/url-verifier-plugin/src/main/java/org/gradle/sample/http/DefaultHttpCaller.java without introducing a dependency to the Gradle API, also in terms of Logging Library to be used.

Or is this question academical? But then again, making basically domain layer code, dependent dependent on the Gradle API, seems to reduce the potential reusability.

Any suggestions, good practice, feedback?

You can use pretty much any logging framework you want. It’ll all get redirected appropriately. I’d probably suggest just using SLF4j, as that’s what Gradle’s API extends from. The only limitation is that you won’t have access to Gradle specific logging levels such as LIFECYCLE or QUIET.

1 Like

Great. I tested this in a Gradle Plugin project of mine and it works.

But in the Integration Tests - tests without any Gradle Dependencies - the following Standard Output is confusing me:

 SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Users/chhex/.gradle/caches/modules-2/files-2.1/org.slf4j/slf4j-simple/1.7.29/82ae07f95088577987a15d90171de12b00d81847/slf4j-simple-1.7.29.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/chhex/.gradle/caches/5.0/generated-gradle-jars/gradle-api-5.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.SimpleLoggerFactory]

This even though i did’t see any gradle dependencies in the htmlDependency Report.

I have a configuration defined as follows:

val integrationTestRuntimeOnly: Configuration by configurations.getting {
       extendsFrom(configurations.testRuntimeOnly.get())
}

and sl4j dependencies declared

dependencies {
    .......
    implementation("org.slf4j:slf4j-api:1.7.25")
    integrationTestRuntimeOnly("org.slf4j:slf4j-simple:1.7.29")
}

So for me it is not quite clear why sl4j is finding multiple bindings, specially the one in the gradle-api

You’re not using gradleApi() for that project?

I am using the GradleApi for this project, it is a Gradle Plugin, but not for the Classes in Test, see the integrationTestRuntimeOnly Configuration.

Can you share how you are setting up your integration test task? Are you using testkit for these tests?

Great thanks for looking into this.
To answer your questions: yes the testKit is a testCompile Dependency
And here the build script: https://github.com/apgsga-it/apg-gradle-plugins/blob/master/dependency-manager/build.gradle.kts
And these the common "applied from: files:
https://github.com/apgsga-it/apg-gradle-plugins/blob/master/gradle-common/common-plugin.gradle
https://github.com/apgsga-it/apg-gradle-plugins/blob/master/gradle-common/functional-test.gradle
https://github.com/apgsga-it/apg-gradle-plugins/blob/master/gradle-common/integration-test.gradle