Gradle : Logging Configuration


Gradle is very rich with logging configuration options for log level, formatting and error stacktrace handling etc.

Gradle logging configuration is required to debug build failure, task execution status and sequence of execution of tasks to build and run projects. It’s formatting style and colors highlight option make build failure debugging easy to understand and resolve issues.

Topics Covered

  • Gradle Log Level Configuration
  • Write Your Own Log Message
  • Gradle Stacktrace Options
  • Gradle Console Log Formatting
  • Gradle Warning Display Options

Gradle Log Level Configuration

Gradle support following log level options, It ordered from least verbose (–debug) to most most verbose (–quiet). Gradle default log level is Lifecycle.

Options Description
-q , –quiet Set Gradle log level QUIET to print errors only.
-w , –warn Set Gradle log level WARN to print warnings and errors.
-i , –info Set Gradle log level INFO to print info, warning and errors.
-d , –debug Set Gradle log level DEBUG to print debug, info, warning and errors.
No Log Option Default log level LIFECYCLE once you not set any. It prints gradle life cycle all statements to execute a task..

There are multiple ways to set Gradle Log Level based on your need:

Set property in gradle.properties in your project root folder or in you GRADLE_HOME folder.

org.gradle.logging.level=debug

or Set Gradle environment variable.

-Dorg.gradle.logging.level=debug

or Pass Gradle log level option on time of execution of tasks. For Example: Passing log level as -w to print logs for warning or errors.

gradlew [task]-w

For Example:

Now takes reference of previous, Gradle Multi Project Application and execute gradle build with out any log level (Default log level is LIFECYCLE)  and builds got executed successfully but not printed any task execution steps.

Gradle multi project build after include sub project

I will take reference of same example for further log settings.

Write Your Own Log Messages

Gradle supports many ways to write your own Log messages in build.gradle file.

  1. Write Log Message by stdout: By default logged these message at QUIET level.
    println 'Logged message at QUIET level'
    
  2. Write Log Message by Logger property: Gradle provide logger property to write message on different log level.
    logger.quiet('Always Logged an info log message.')
    logger.error('Logged an error message.')
    logger.warn('Logged  a warning message.')
    logger.lifecycle('Logged  a lifecycle info message.')
    logger.info('Logged an info message.')
    logger.debug('Logged a debug message.')
    logger.trace('Logged a trace message.')
    
  3.  Write Log Message with place holder: Use curly braces ({}) for place holder.
    logger.info('Test a place holder {} log message', 'info')
    

Now for test your log level configuration. Write these state debug statements in your build.gradle file.

//top level log stdout
println 'Logged message at QUIET level'
 
task logInfo {
    //task debug
    logger.quiet('Always Logged an info log message.')
    logger.error('Logged an error message.')
    logger.warn('Logged  a warning message.')
    logger.lifecycle('Logged  a lifecycle info message.')
    logger.info('Logged an info message.')
    logger.debug('Logged a debug message.')
    logger.trace('Logged a trace message.')
    
    //test place holder
    logger.info('Test a place holder {} log message', 'info')
}

Run these command on console and verify your results.

Gradle Log Level

In above example, running specific logInfo task to check our configuration. Based on passing log level option it’s showing log statements in console.

Gradle Stacktrace Options

By default Gradle doesn’t print stacktrace in logs if case build get fail. To enable stacktrace use these options.

Options Description
-s or –stacktrace Use this option when need to get truncated stacktrace. It reduces the unnecessary verbose statements.
-S or –full-stacktrace Use this option when need print complete stacktrace and it’s also print stacktrace for deprecated warnings.

Gradle Console Log Formatting

Gradle is rich with console log formatting.  Gradle console mode provide some additional features while build and run:

  • Use colors and fonts to highlight log level, important outputs and errors.
  • Show Progress lines and what execution is going currently.
  • Progress bar and timer describe the overall execution status.
Options Description
plain This option disable all color and rich output in console log output. This is consider as default option when Gradle not attached to a terminal.
auto This is default option to enable color and other rich output when attached to a terminal.
rich This provide rich output in form of color, fonts and also support ANSI control characters.
verbose It’s use to enable color and other rich output like the rich, but print task names and out comes at the lifecycle log level.

There are multiple ways to set Gradle console log formatting options as below:

Set property in gradle.properties in your project root folder or in you GRADLE_HOME folder.

org.gradle.console=plain

or Set as system environment variable from command prompt.

-Dorg.gradle.console=plain

To pass Gradle console log  option on time of execution of tasks. For Example :Here by default log level is ‘–lifecycle’  and set console log option as plain (‘–console=plain’) to print logs console..

gradlew [task]--console=plain

Now build same previous, Gradle Multi Project Application with console option as ‘–console=plain’ and see the difference it’s printing all steps followed for execution of tasks.

Multi project all task in console

As an assignment you can try below line of statements to test different log level and see console outputs.

//Log Level as debubg
gradlew [task] -d --console=plain
//Log Level as info
gradlew [task]-i --console=plain

Gradle Warnings Display Options

Gradle doesn’t show warnings (e.g deprecated warnings) in sequence . Gradle collects all warnings and show a summary at end of builds:

For Example

Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.

Gradle provides these options to show/ hide warnings in console log.

Options Description
all To log all warnings.
fail To log all warnings and fails in build.
summary To log summary suppress all warnings at end of build.
none To suppress all warnings and summary at end of build.

Gradle provides multiple ways to set property to show/hide gradle warnings:

Set property in gradle.properties in your project root folder or in you GRADLE_HOME folder.

org.gradle.warning.mode=all

or Set property as gradle environment variable.

-Dorg.gradle.warning.mode=all

or Pass warning mode on time on execution of task.

gradlew [task] --warning-mode=all

Conclusion

Here you have learn about gradle log level  configuration on project , tasks and centralize level. Here are these options:

  • Gradle Log Level:  Use these  different log level to debug gradle build.
    org.gradle.logging.level=debug|info|warn|quit
    
  • Gradle Stacktrace Options: Use -s for truncated stacktrace and -S for full stacktrace.
  • Gradle Console Log Formatting: For  formatted colored logs and  get execution progress status use  these console log options.
    org.gradle.console=plain|auto|rich|verbose
    
  • Gradle Warning show/hide: Use these  options for show and hide warnings.
    org.gradle.warning.mode=all|fail|summary|none
    

Leave a comment