Logging

Until now we have used println everywhere in the build script to display the messages to the user. If you are coming from a Java background you know a println statement is not the right way to give information to the user. You need logging. Logging helps the user to classify the categories of messages to show at different levels. These different levels help users to print a correct message based on the situation. For example, when a user wants complete detailed tracking of your software, they can use debug level. Similarly, whenever a user wants very limited useful information while executing a task, they can use quiet or info level. Gradle provides the following different types of logging:

Log Level

Description

ERROR

This is used to show error messages

QUIET

This is used to show limited useful information

WARNING

This is used to show warning messages

LIFECYCLE

This is used to show the progress (default level)

INFO

This is used to show information messages

DEBUG

This is used to show debug messages (all logs)

By default, the Gradle log level is LIFECYCLE. The following is the code snippet from LogExample/build.gradle:

task showLogging << {
  println "This is println example"
  logger.error "This is error message"
  logger.quiet "This is quiet message"
  logger.warn "This is WARNING message"
  logger.lifecycle "This is LIFECYCLE message"
  logger.info "This is INFO message"
  logger.debug "This is DEBUG message"
}

Now, execute the following command:

$ gradle showLogging

:showLogging
This is println example
This is error message
This is quiet message
This is WARNING message
This is LIFECYCLE message

BUILD SUCCESSFUL

Here, Gradle has printed all the logger statements upto the lifecycle level (including lifecycle), which is Gradle's default log level. You can also control the log level from the command line.

-q

This will show logs up to the quiet level. It will include error and quiet messages

-i

This will show logs up to the info level. It will include error, quiet, warning, lifecycle and info messages.

-s

This prints out the stacktrace for all exceptions.

-d

This prints out all logs and debug information. This is most expressive log level, which will also print all the minor details.

Now, execute gradle showLogging -q:

This is println example
This is error message
This is quiet message

Apart from the regular lifecycle, Gradle provides an additional option to provide stack trace in case of any exception. Stack trace is different from debug. In case of any failure, it allows tracking of all the nested functions, which are called in sequence up to the point where the stack trace is generated.

To verify, add the assert statement in the preceding task and execute the following:

task showLogging << {
println "This is println example"
..
assert 1==2
}
$ gradle showLogging -s
……
* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':showLogging'.
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:69)
       at
….
org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:53)
       at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)
       at org.gradle.api.internal.AbstractTask.executeWithoutThrowingTaskFailure(AbstractTask.java:305)
...

With stracktrace, Gradle also provides two options:

  • -s or --stracktrace: This will print truncated stracktrace
  • -S or --full-stracktrace: This will print full stracktrace
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.172.115