androidandroid-studiogradlebuildkite

Custom Gradle Logging


My company uses Buildkite for our CI services. When the CI server triggers a build, all logs from the build are sent to Buildkite and made available through the UI. This is useful since it allows us to check how a build failed.

Buildkite has a feature which creates collapsible groups from logs formatted like:

--- Compiling
logs after this point will be in a collapsible group named 'Compiling'

--- Testing
logs after this point will be in a collapsible group named 'Testing'

How can I add custom logging to a gradle build which will output these 'groups'? I'd like to have a group for compiling/assembling, one for running unit tests, etc.

I've considered adding tasks which perform the logging and making them dependencies of the built-in tasks, but I'm not sure how to do that, or if it's a good idea in general.


Solution

  • I'm using Gradle 2.12 and whipped up an example from the Logging doc. The example does not use Android Studio nor BuildKite, but I believe this will help with the fundamental question.

    Given a simple build.gradle file for a typical Java project:

    apply plugin: 'java'
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        testCompile 'junit:junit:4.11'
    }
    
    compileJava << { println "TRACER example log from compileJava" }
    compileTestJava << { println "TRACER example log from compileTestJava" }
    
    test << { println "TRACER example log from test" }
    

    and an init.gradle file:

    useLogger(new CustomEventLogger())
    
    class CustomEventLogger extends BuildAdapter implements TaskExecutionListener {
    
        public void beforeExecute(Task task) {
            if (task.name ==~ "compileJava") {
                println "--- Compiling"
            } else if (task.name == "test") {
                println "--- Testing"
            }
        }
    
        public void afterExecute(Task task, TaskState state) {}
    
        public void buildFinished(BuildResult result) {}
    }
    

    then this command-line:

    $ gradle -I init.gradle test
    

    yields this output:

    --- Compiling
    TRACER example log from compileJava
    TRACER example log from compileTestJava
    --- Testing
    TRACER example log from test