Setting Up Cloud Logging for Java

You can write logs to Cloud Logging from Java applications by using the Logback appender or a java.util.logging handler, or by using the Cloud Logging library for Java directly.

The Cloud Logging agent does not have to be installed to use the Cloud Logging library for Java.

Before you begin

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud Console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Verifica che la fatturazione sia attivata per il tuo progetto.

    scopri come attivare la fatturazione

  4. Enable the Cloud Logging API.

    Enable the API

Logback appender for Cloud Logging

Using the Logback appender, you can use Cloud Logging with the SLF4J logging facade.

Installing the dependency

If you are using Maven, add the following to your pom.xml file. For more information about BOMs, see The Google Cloud Platform Libraries BOM.


If you are using Gradle, add the following to your dependencies:

compile ''

If you are using sbt, add the following to your dependencies:

libraryDependencies += "" % "google-cloud-logging-logback" % "0.120.8-alpha"

Logback configuration

Logback can be configured programmatically or using a script expressed in XML or Groovy.

You can customize the minimum severity threshold, log name, or provide additional enhancers. This is a sample Logback configuration in XML format:

  <appender name="CLOUD" class="">
    <!-- Optional : filter logs at or above a level -->
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
    <log>application.log</log> <!-- Optional : default java.log -->
    <resourceType>gae_app</resourceType> <!-- Optional : default: auto-detected, fallback: global -->
    <enhancer>com.example.logging.logback.enhancers.ExampleEnhancer</enhancer> <!-- Optional -->
    <flushLevel>WARN</flushLevel> <!-- Optional : default ERROR -->

  <root level="info">
    <appender-ref ref="CLOUD" />


Once you have configured Logback to use the Cloud Logging Logback appender, you can now redirect logs using the SLF4J logging API. This snippet shows how to log using SLF4J facade within your application:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Quickstart {
  private static final Logger logger = LoggerFactory.getLogger(Quickstart.class);

  public static void main(String[] args) {"Logging INFO with Logback");
    logger.error("Logging ERROR with Logback");

The java.util.logging handler

You can also use the Cloud Logging API using a default Java Logging API handler.

Installing the dependency

If you are using Maven with a BOM, add the following to your pom.xml file:


  <!-- ...

If you are using Maven without a BOM, add this to your dependencies:


If you are using Gradle, add the following to your dependencies:

implementation platform('')

compile ''

If you are using sbt, add the following to your dependencies:

libraryDependencies += "" % "google-cloud-logging" % "2.2.3"

If you're using IntelliJ or Eclipse, you can add client libraries to your project using the following IDE plugins:

The plugins provide additional functionality, such as key management for service accounts. Refer to each plugin's documentation for details.

java.util.logging configuration

Logging handlers can be added programmatically or by using a configuration file. The path to the configuration file must be provided to your application as a system property: -Djava.util.logging.config.file=/path/to/

Here is an example of a configuration file:

# To use this configuration, add to system properties : -Djava.util.logging.config.file="/path/to/file"
.level = INFO

# it is recommended that io.grpc and logging level is kept at INFO level,
# as both these packages are used by Cloud internals and can result in verbose / initialization problems.
# default : java.log

# default : INFO

# default : ERROR

# default : auto-detected, fallback "global"

# custom formatter
java.util.logging.SimpleFormatter.format=%3$s: %5$s%6$s

#optional enhancers (to add additional fields, labels)


This snippet shows you how to log using java.util.logging:

import java.util.logging.Logger;

public class Quickstart {
  private static final Logger logger = Logger.getLogger(Quickstart.class.getName());

  public static void main(String[] args) {"Logging INFO with java.util.logging");
    logger.severe("Logging ERROR with java.util.logging");

Common Configuration

The following sections cover configuration that is common to the java.util.logging handler and the Logback appender for Cloud Logging.


The Logback appender and java.util.logging handler use the following defaults to instantiate a Cloud Logging client:

  • Log name : java.log

  • Minimum threshold to log : INFO

  • Flush severity : ERROR

The Cloud Logging library for Java batches messages by size and time since last write. Batches with logging requests at or above the flush severity are immediately written out.

Monitored resource detection

All logs sent via the Cloud Logging libraries require a monitored resource type to identify your application.

The Logback appender and the java.util.logging handler provide automatic resource type detection of your App Engine, Compute Engine, and Google Kubernetes Engine applications.

A global monitored resource is used as the default in other environments.

You can override the monitored resource type to a valid type in the Logback appender Configuration or the java.util.logging Handler Configuration.

Additional fields and labels

Using the Logback appender and the java.util.logging handler, you can add or update fields on a LogEntry object using an instance of LoggingEnhancer.

The enhancers must be configured as shown in the Logback appender configuration or the java.util.logging handler configuration:


// Add / update additional fields to the log entry
public class ExampleEnhancer implements LoggingEnhancer {

  public void enhanceLogEntry(LogEntry.Builder logEntry) {
    // add additional labels
    logEntry.addLabel("test-label-1", "test-value-1");

For more information on installation, see the documentation for the Cloud Logging library for Java. You can also report issues using the issue tracker.

Using the Cloud Client Library directly

For information on using the Cloud Logging Cloud client library for Java directly, see Cloud Logging Client Libraries.

Running on Google Cloud

Using Cloud Logging library for Java requires the IAM Logs Writer role on Google Cloud. Most Google Cloud environments provide this role by default.

App Engine

App Engine grants the Logs Writer role by default.

The Cloud Logging library for Java can be used without needing to explicitly provide credentials.

Cloud Logging is automatically enabled for App Engine applications. No additional setup is required.

App Engine standard environment, by default, uses the java.util.logging.Logger API; this writes to Cloud Logging directly and is easy to configure.

For further details, see App Engine documentation on Reading and Writing Application Logs.

App Engine flexible environment

On App Engine flexible environment, java.util.logging uses the ConsoleHandler by default, and sends logs to stdout and stderr.

The Jetty Runtime is bundled with the Cloud Logging library for Java.

The java.util.logging handler can be used to log directly to Cloud Logging by providing the in your app.yaml as shown here:

      JETTY_ARGS: -Djava.util.logging.config.file=WEB-INF/

Trace ID logging is available on Jetty runtimes if you are using the java.util.logging handler or the Logback appender.

When running on App Engine flexible environment, a TraceLoggingEnhancer instance adds a thread-safe trace ID to every log entry using the label trace_id.

Google Kubernetes Engine

On Google Kubernetes Engine, you must add the logging.write access scope when creating the cluster:

gcloud container clusters create example-cluster-name --scopes

Compute Engine

When using Compute Engine VM instances, add the cloud-platform access scope to each instance. When creating a new instance through the Google Cloud Console, you can do this in the Identity and API access section of the Create Instance panel. Use the Compute Engine default service account or another service account of your choice, and select Allow full access to all Cloud APIs in the Identity and API access section. Whichever service account you select, ensure that it has been granted the Logs Writer role in the IAM & Admin section of the Cloud Console.

Running locally and elsewhere

To use the Cloud Logging library for Java outside of Google Cloud, including running the library on your own workstation, on your data center's computers, or on the VM instances of another cloud provider, you must supply your Google Cloud project ID and appropriate service account credentials directly to the Cloud Logging library for Java.

You can create and obtain service account credentials manually. When specifying the Role field, use the Logs Writer role. For more information on Identity and Access Management roles, go to Access control guide.

Viewing the logs

After deployment, you can view the logs in the Logs Explorer.

Go to the Logs Explorer

In the Logs Explorer, you must specify one or more resources, but the resource selection might not be obvious. Here are some tips to help you get started:

  • If you are deploying your application to App Engine or using the App Engine-specific libraries, set your resource to GAE Application.

  • If you are deploying your application on Compute Engine, set the resource to GCE VM Instance.

  • If you are deploying your application on Google Kubernetes Engine, your cluster's logging configuration determines the resource type of the log entries. For a detailed discussion on the Legacy Google Cloud's operations suite and the Google Cloud's operations suite Kubernetes Monitoring solutions, and how those options affect the resource type, see Migrating to Google Cloud's operations suite Kubernetes Monitoring.

  • If your application is using the Cloud Logging API directly, the resource is dependent on the API and your configuration. For example, in your application, you can specify a resource or use a default resource.

  • If you don't see any logs in the Logs Explorer, to see all log entries, switch to the advanced query mode and use an empty query.

    1. To switch to the advanced query mode, click menu (▾) at the top of the Logs Explorer and then select Convert to advanced filter.
    2. Clear the content that appears in the filter box.
    3. Click Submit Filter.

    You can examine the individual entries to identify your resources.

For additional information, see Viewing Logs and Advanced logs queries.