https://gradle.com/ logo
a

Andres Almiray

07/26/2022, 1:05 PM
Does anyone know how to inject a logging service into a
WorkerAction
subclass?
m

melix

07/26/2022, 1:21 PM
I'm not aware of such a service. I think you could use slf4j in your action, though.
a

Andres Almiray

07/26/2022, 1:24 PM
But would that connect with the project’s logger settings?
m

melix

07/26/2022, 1:34 PM
So I have tested a little script and unfortunately it seems that the logger is not bound to the project logger, so logs are swallowed it seems 😞
println
works but meh
v

Vampire

07/26/2022, 1:39 PM
Do you use process isolation or a different level? (you both)
Because slf4j should work iirc
m

melix

07/26/2022, 1:39 PM
my scrpt uses
noIsolation()
v

Vampire

07/26/2022, 1:39 PM
And there is no injectable logging service yet, no
m

melix

07/26/2022, 1:40 PM
test script:
Copy code
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.slf4j:slf4j-api:1.7.25'
    }
}

interface MyWorkParams extends WorkParameters {
    Property<String> getName()
}

abstract class SayHello implements WorkAction<MyWorkParams> {
    
    private static final Logger LOGGER = org.slf4j.LoggerFactory.getLogger(SayHello)
    
    void execute() {
        LOGGER.debug("Hello, {}!", getParameters().getName().get())
    }
}

abstract class SayHelloTask extends DefaultTask {
    @Inject
    abstract WorkerExecutor getWorkerExecutor()

    @TaskAction
    void execute() {
        workerExecutor.noIsolation()
            .submit(SayHello) {
                name.set("Cédric")
            }
    }
}

tasks.register("hello", SayHelloTask)
calling
hello
shows nothing
a

Andres Almiray

07/26/2022, 1:42 PM
I mean, how else would a
WorkerAction
pass information to console/log which is compatible with the build’s logging settings? Sure I can use
println
but, … if so, what’s the point of logging APIs then?
v

Vampire

07/26/2022, 1:42 PM
Well, did you enable debug logging @melix? Otherwise it of course does not show anything.
The user guide says
You can also hook into Gradle’s logging system from within other classes used in the build (classes from the
buildSrc
directory for example). Simply use an SLF4J logger. You can use this logger the same way as you use the provided logger in the build script.
m

melix

07/26/2022, 1:42 PM
I tried info, trace and debug
v

Vampire

07/26/2022, 1:43 PM
And did you enable the level?
info is only shown with
--info
or
--debug
trace and debug is only shown with
--debug
m

melix

07/26/2022, 1:43 PM
no, because I'm looking for a log level which would bind to
lifecycle
so that it shows something like
println
v

Vampire

07/26/2022, 1:44 PM
The default logging level is LIFECYCLE
m

melix

07/26/2022, 1:44 PM
warn actually works
v

Vampire

07/26/2022, 1:44 PM
That log level is not a standard log level, it is only available with the Gradle own logger subclass
stdout is by default bound to lifecycle if not reconfigured
m

melix

07/26/2022, 1:44 PM
I know, but if you want to avoid
println
, you have to find one level which works 😄
v

Vampire

07/26/2022, 1:45 PM
Or you cannot use slf4j but have to use the Gradle logger
You can probably use
Logging.getLogger
to get a Gradle logger where you can use
lifecycle
But that level (and higher) should be used sparsely anyway. 🙂
m

melix

07/26/2022, 1:46 PM
so, verified: •
info
-> logged if you run with
-i
warn
-> logged as lifecycle •
debug
-> logged if you run with
-d
v

Vampire

07/26/2022, 1:46 PM
Gradle used to log much more information by default, but at some point shifted more and more output to
--info
or below to not clutter the output with info logs and thus better see important messages like warnings and errors.
No, warn is not logged as lifecycle
warn is logged as warn
You see the levels if you use
--debug
m

melix

07/26/2022, 1:47 PM
ah right. But at least shows something on the console.
v

Vampire

07/26/2022, 1:47 PM
Also don't forget
--quiet
;-)
m

melix

07/26/2022, 1:47 PM
logging in Gradle is a mess
tbh
v

Vampire

07/26/2022, 1:47 PM
Well, ..., yes
Kind of
m

melix

07/26/2022, 1:47 PM
like no way to only log a single task type
it's debug all the things or die 😄
but at least, Andrés, you have your answer: slf4j works
a

Andres Almiray

07/26/2022, 1:49 PM
Thanks. I chose
Logging.getLogger()
m

melix

07/26/2022, 1:49 PM
works too?
a

Andres Almiray

07/26/2022, 1:49 PM
Yes, just tried it
m

melix

07/26/2022, 1:49 PM
interesting, I had no idea Gradle supported j.u.l
v

Vampire

07/26/2022, 1:49 PM
That's a Gradle class
a

Andres Almiray

07/26/2022, 1:49 PM
correct, it’s
org.gradle.api.logging.Logging
v

Vampire

07/26/2022, 1:50 PM
Just what I suggested some messages above
m

melix

07/26/2022, 1:50 PM
ahhh, mixed with
Logger
.
TooManyLoggersException
v

Vampire

07/26/2022, 1:50 PM
It works fine as long as you have the Gradle APIs available and write Gradle specific code.
a

Andres Almiray

07/26/2022, 1:50 PM
My gut feeling tells me when there are too many options, go with the Gradle flavor class, as this is the case. thanks @Vampire
3 Views