7

Until gradle 3, I used this to write gradle's output to a logfile:

def fileLogger = [
    onOutput : {
        File logfile = new File( 'gradle.log' )
        logfile << it
    }
] as org.gradle.api.logging.StandardOutputListener

gradle.useLogger( fileLogger )

This does not work with with gradle 4.

elsamuko
  • 668
  • 6
  • 16

1 Answers1

7

Update for Gradle 5: It works when using logging.addStandardOutputListener instead gradle.useLogger and adding it to all tasks:

// logger
def fileLogger = [
        onOutput: {
            File logfile = new File('gradle.log')
            logfile << it
        }
] as org.gradle.api.logging.StandardOutputListener

// for configuration phase
logging.addStandardOutputListener(fileLogger)

// for execution phase
gradle.taskGraph.whenReady { taskGraph ->
    taskGraph.allTasks.each { Task t ->
        t.doFirst {
            logging.addStandardOutputListener(fileLogger)
        }
    }
}
elsamuko
  • 668
  • 6
  • 16
  • You might want to put that in a task to prevent its execution during configuration all the time. For a build server build the cost is well worth it. For an IDEA triggered build it may needlessly slow things down. Been there, done that. – Peter Kahn Nov 07 '19 at 23:35