# Logging

version 7.3

The log is the main 'UI' of a build tool. If it is too verbose, real warnings and problems are easily hidden by this. On the other hand you need relevant information for figuring out if things have gone wrong. Gradle defines 6 log levels, as shown in Log levels. There are two Gradle-specific log levels, in addition to the ones you might normally see. Those levels are QUIET and LIFECYCLE. The latter is the default, and is used to report build progress.

## Log levels

ERROR | Error messages
QUIET | Important information messages
WARNING | Warning messages
LIFECYCLE | Progress information messages
INFO | Information messages
DEBUG | Debug messages

| The rich components of the console (build status and work in progress area) are displayed regardless of the log level used. Before Gradle 4.0 those rich components were only displayed at log level `LIFECYCLE` or below.

## Choosing a log level

You can use the command line switches shown in Log level command-line options to choose different log levels. You can also configure the log level using `gradle.properties`, see Gradle properties. In Stacktrace command-line options you find the command line switches which affect stacktrace logging.

Table 1. Log level command-line options
Option | Outputs Log Levels
--- | ---
no logging options | LIFECYCLE and higher
`-q` or `--quiet` | QUIET and higher
`-w` or `--warn` | WARN and higher
`-i` or `--info` | INFO and higher
`-d` or `--debug` | DEBUG and higher (that is, all log messages)

| The `DEBUG` log level can expose security sensitive information to the console.

### Stacktrace command-line options

`-s` or `--stacktrace`

  Truncated stacktraces are printed. We recommend this over full stacktraces. Groovy full stacktraces are extremely verbose (Due to the underlying dynamic invocation mechanisms. Yet they usually do not contain relevant information for what has gone wrong in your code.) This option renders stacktraces for deprecation warnings.

`-S` or `--full-stacktrace`

  The full stacktraces are printed out. This option renders stacktraces for deprecation warnings.

<No stacktrace options>

  No stacktraces are printed to the console in case of a build error (e.g. a compile error). Only in case of internal exceptions will stacktraces be printed. If the `DEBUG` log level is chosen, truncated stacktraces are always printed.

## Logging Sensitive Information

Running Gradle with the `DEBUG` log level can expose security sensitive information to the console and build log.

This information can include but is not limited to:

- Environment variables

- Private repository credentials

- Build cache & Gradle Enterprise Credentials

- Plugin Portal publishing credentials

The DEBUG log level should not be used when running on public Continuous Integration services. Build logs for public Continuous Integration services are world-viewable and can expose this sensitive information. Depending upon your organization’s threat model, logging sensitive credentials in private CI may also be a vulnerability. Please discuss this with your organization’s security team.

Some CI providers attempt to scrub sensitive credentials from logs; however, this will be imperfect and usually only scrubs exact-matches of pre-configured secrets.

If you believe a Gradle Plugin may be exposing sensitive information, please contact security@gradle.com for disclosure assistance.

## Writing your own log messages

A simple option for logging in your build file is to write messages to standard output. Gradle redirects anything written to standard output to its logging system at the `QUIET` log level.

Example 1. Using stdout to write log messages
build.gradle

    println 'A message which is logged at QUIET level'

build.gradle.kts

    println("A message which is logged at QUIET level")

Gradle also provides a `logger` property to a build script, which is an instance of Logger. This interface extends the SLF4J `Logger` interface and adds a few Gradle specific methods to it. Below is an example of how this is used in the build script:

Example 2. Writing your own log messages
build.gradle

    logger.quiet('An info log message which is always logged.')
    logger.error('An error log message.')
    logger.warn('A warning log message.')
    logger.lifecycle('A lifecycle info log message.')
    logger.info('An info log message.')
    logger.debug('A debug log message.')
    logger.trace('A trace log message.') // Gradle never logs TRACE level logs

build.gradle.kts

    logger.quiet("An info log message which is always logged.")
    logger.error("An error log message.")
    logger.warn("A warning log message.")
    logger.lifecycle("A lifecycle info log message.")
    logger.info("An info log message.")
    logger.debug("A debug log message.")
    logger.trace("A trace log message.") // Gradle never logs TRACE level logs

Use the typical SLF4J pattern to replace a placeholder with an actual value as part of the log message.

Example 3. Writing a log message with placeholder
build.gradle

    logger.info('A {} log message', 'info')

build.gradle.kts

    logger.info("A {} log message", "info")

You can also hook into Gradle’s logging system from within other classes used in the build (classes from the `buildSrc` directory for example). Simply use an SLF4J logger. You can use this logger the same way as you use the provided logger in the build script.

Example 4. Using SLF4J to write log messages
build.gradle

    import org.slf4j.LoggerFactory

    def slf4jLogger = LoggerFactory.getLogger('some-logger')
    slf4jLogger.info('An info log message logged using SLF4j')

build.gradle.kts

    import org.slf4j.LoggerFactory

    val slf4jLogger = LoggerFactory.getLogger("some-logger")
    slf4jLogger.info("An info log message logged using SLF4j")

## Logging from external tools and libraries

Internally, Gradle uses Ant and Ivy. Both have their own logging system. Gradle redirects their logging output into the Gradle logging system. There is a 1:1 mapping from the Ant/Ivy log levels to the Gradle log levels, except the Ant/Ivy `TRACE` log level, which is mapped to Gradle `DEBUG` log level. This means the default Gradle log level will not show any Ant/Ivy output unless it is an error or a warning.

There are many tools out there which still use standard output for logging. By default, Gradle redirects standard output to the `QUIET` log level and standard error to the `ERROR` level. This behavior is configurable. The project object provides a LoggingManager, which allows you to change the log levels that standard out or error are redirected to when your build script is evaluated.

Example 5. Configuring standard output capture
build.gradle

    logging.captureStandardOutput LogLevel.INFO
    println 'A message which is logged at INFO level'

build.gradle.kts

    logging.captureStandardOutput(LogLevel.INFO)
    println("A message which is logged at INFO level")

To change the log level for standard out or error during task execution, tasks also provide a LoggingManager.

Example 6. Configuring standard output capture for a task
build.gradle

    tasks.register('logInfo') {
        logging.captureStandardOutput LogLevel.INFO
        doFirst {
            println 'A task message which is logged at INFO level'
        }
    }

build.gradle.kts

    tasks.register("logInfo") {
        logging.captureStandardOutput(LogLevel.INFO)
        doFirst {
            println("A task message which is logged at INFO level")
        }
    }

Gradle also provides integration with the Java Util Logging, Jakarta Commons Logging and Log4j logging toolkits. Any log messages which your build classes write using these logging toolkits will be redirected to Gradle’s logging system.

## Changing what Gradle logs

You can replace much of Gradle’s logging UI with your own. You might do this, for example, if you want to customize the UI in some way - to log more or less information, or to change the formatting. You replace the logging using the Gradle.useLogger(java.lang.Object) method. This is accessible from a build script, or an init script, or via the embedding API. Note that this completely disables Gradle’s default output. Below is an example init script which changes how task execution and build completion is logged.

Example 7. Customizing what Gradle logs
customLogger.init.gradle

    useLogger(new CustomEventLogger())

    class CustomEventLogger extends BuildAdapter implements TaskExecutionListener {

        void beforeExecute(Task task) {
            println "[$task.name]"
        }

        void afterExecute(Task task, TaskState state) {
            println()
        }

        void buildFinished(BuildResult result) {
            println 'build completed'
            if (result.failure != null) {
                result.failure.printStackTrace()
            }
        }
    }

customLogger.init.gradle.kts

    useLogger(CustomEventLogger())

    class CustomEventLogger() : BuildAdapter(), TaskExecutionListener {

        override fun beforeExecute(task: Task) {
            println("[${task.name}]")
        }

        override fun afterExecute(task: Task, state: TaskState) {
            println()
        }

        override fun buildFinished(result: BuildResult) {
            println("build completed")
            if (result.failure != null) {
                (result.failure as Throwable).printStackTrace()
            }
        }
    }

    $ gradle -I customLogger.init.gradle build

    > Task :compile
    [compile]
    compiling source

    > Task :testCompile
    [testCompile]
    compiling test source

    > Task :test
    [test]
    running unit tests

    > Task :build
    [build]

    build completed
    3 actionable tasks: 3 executed

    $ gradle -I customLogger.init.gradle.kts build

    > Task :compile
    [compile]
    compiling source

    > Task :testCompile
    [testCompile]
    compiling test source

    > Task :test
    [test]
    running unit tests

    > Task :build
    [build]

    build completed
    3 actionable tasks: 3 executed

Your logger can implement any of the listener interfaces listed below. When you register a logger, only the logging for the interfaces that it implements is replaced. Logging for the other interfaces is left untouched. You can find out more about the listener interfaces in Build lifecycle events.

- BuildListener

- ProjectEvaluationListener

- TaskExecutionGraphListener

- TaskExecutionListener

- TaskActionListener