Setting Up Stackdriver Logging for Java

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

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

Before you begin

  1. Sign in to your Google Account.

    If you don't already have one, sign up for a new account.

  2. Select or create a GCP project.

    Go to the Manage resources page

  3. Make sure that billing is enabled for your project.

    Learn how to enable billing

  4. Enable the Stackdriver Logging API.

    Enable the API

Logback appender for Stackdriver Logging

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

Installing the dependency

If you are using Maven, add this to your pom.xml file:
If you are using Gradle, add this to your dependencies:
compile ''
If you are using SBT, add this to your dependencies:
libraryDependencies += "" % "google-cloud-logging-logback" % "0.53.0-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 Stackdriver 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 Stackdriver Logging API using a default Java Logging API handler.

Installing the dependency

If you are using Maven, add this to your pom.xml file:
If you are using Gradle, add this to your dependencies:
compile ''
If you are using SBT, add this to your dependencies:
libraryDependencies += "" % "google-cloud-logging" % "1.35.0"

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:


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 Stackdriver 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 Stackdriver Logging.


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

  • Log name : java.log

  • Minimum threshold to log : INFO

  • Flush severity : ERROR

The Stackdriver 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 Stackdriver 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 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 Stackdriver Logging library for Java. You can also report issues using the issue tracker.

Using the Cloud Client Library directly

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

Running on Google Cloud Platform

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

App Engine

Google App Engine grants the Logs Writer role by default.

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

Stackdriver 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 Stackdriver 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 Stackdriver Logging library for Java.

The java.util.logging handler can be used to log directly to Stackdriver 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.

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 Google Compute Engine VM instances, add the cloud-platform access scope to each instance. When creating a new instance through the Google Cloud Platform 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 GCP Console.

Running locally and elsewhere

To use the Stackdriver Logging library for Java outside of Google Cloud Platform, you must supply your GCP project ID and appropriate service account credentials directly to the Stackdriver Logging library for Java. This applies to running the agent on your own workstation, on your data center's computers, or on the VM instances of another cloud provider. For more information see Obtaining and providing service account credentials manually.

Viewing the logs

After deployment, you can view the logs in the GCP Console Logs Viewer.

Go to the Logs Viewer

For more information, see Viewing Logs.

Var denne side nyttig? Giv os en anmeldelse af den:

Send feedback om...

Stackdriver Logging