Gradle Logging: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(8 intermediate revisions by the same user not shown)
Line 4: Line 4:
=Overview=
=Overview=


Gradle exposes access to its internal logging via the <code>logger<code> variable, which is available from [[settings.gradle]] and [[build.gradle]]. In build.gradle, the variable corresponds to [https://docs.gradle.org/current/javadoc/org/gradle/api/Project.html#getLogger-- Project.getLogger()] accessor. The underlying logging component exposes the standard SLF4J "error", "warn", "info", "debug" methods, as well as a few extra ones, like "quiet".
Gradle exposes access to its internal logging mechanism via the <code>logger</code> variable, which is available from [[settings.gradle]] and [[build.gradle]]. In build.gradle, the variable corresponds to [https://docs.gradle.org/current/javadoc/org/gradle/api/Project.html#getLogger-- Project.getLogger()] accessor. The underlying logging component exposes the standard SLF4J "error", "warn", "info", "debug" and "trace" methods, as well as a few extra ones, like "quiet".


<syntaxhighlight lang='groovy'>  
<syntaxhighlight lang='groovy'>  
Line 12: Line 12:
logger.info "this is info content will be sent to stdout when --info is active"
logger.info "this is info content will be sent to stdout when --info is active"
logger.debug "this is debug content will be sent to stdout when --debug is active"
logger.debug "this is debug content will be sent to stdout when --debug is active"
logger.trace "test"
</syntaxhighlight>
=Output Configuration=
The logging level can be controlled on command line with command line flags ([[Gradle_Command_Line#--q.2C--quiet|-q|--quiet]], [[Gradle_Command_Line#-i.2C--info|-i|--info]], [[Gradle_Command_Line#-d.2C--debug|-d|--debug]]).
=Accessing Gradle Logging in Custom Java Tasks=
For a [[Extending_Gradle_with_a_Custom_Enhanced_Task#Logging|custom Java task]], access to the Gradle logging can achieved as following:
<syntaxhighlight lang='java'>
[...]
import org.gradle.api.DefaultTask;
import org.gradle.api.logging.Logger;
[...]
public class ExampleTask extends DefaultTask {
  private final Logger logger;
  public IncrementalBuildAwareTask() {
      this.logger = getProject().getLogger();
  }
  [...]
}
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 01:00, 19 October 2020

Internal

Overview

Gradle exposes access to its internal logging mechanism via the logger variable, which is available from settings.gradle and build.gradle. In build.gradle, the variable corresponds to Project.getLogger() accessor. The underlying logging component exposes the standard SLF4J "error", "warn", "info", "debug" and "trace" methods, as well as a few extra ones, like "quiet".

 
logger.quiet "this is info content that will always be sent to the stdout"
logger.error "this is error content that will be sent to stderr"
logger.warn "this is warning content will be sent to stdout"
logger.info "this is info content will be sent to stdout when --info is active"
logger.debug "this is debug content will be sent to stdout when --debug is active"
logger.trace "test"

Output Configuration

The logging level can be controlled on command line with command line flags (-q|--quiet, -i|--info, -d|--debug).

Accessing Gradle Logging in Custom Java Tasks

For a custom Java task, access to the Gradle logging can achieved as following:

[...]
import org.gradle.api.DefaultTask;
import org.gradle.api.logging.Logger;

[...]
public class ExampleTask extends DefaultTask {

   private final Logger logger;

   public IncrementalBuildAwareTask() {
      this.logger = getProject().getLogger();
   }

   [...]
}