Setting Up Stackdriver Logging for Go

We provide a standard library-compatible logging package. The Stackdriver Logging library for Go exposes a higher-level layer for working with Stackdriver Logging.

The Stackdriver Logging agent does not have to be installed to use the Stackdriver Logging library for Go on a Compute Engine VM instance.

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

Installing the package

go get -u

Using the Go logger

// Sample stdlogging writes log.Logger logs to the Stackdriver Logging.
package main

import (

	// Imports the Stackdriver Logging client package.

func main() {
	ctx := context.Background()

	// Sets your Google Cloud Platform project ID.
	projectID := "YOUR_PROJECT_ID"

	// Creates a client.
	client, err := logging.NewClient(ctx, projectID)
	if err != nil {
		log.Fatalf("Failed to create client: %v", err)
	defer client.Close()

	// Sets the name of the log to write to.
	logName := "my-log"

	logger := client.Logger(logName).StandardLogger(logging.Info)

	// Logs "hello world", log entry is visible at
	// Stackdriver Logs.
	logger.Println("hello world")

For more information on installation, see the documentation for the Stackdriver Logging library for Go. 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 Go directly, see Stackdriver Logging Client Libraries.

Running on Google Cloud Platform

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

App Engine

App Engine grants the Logs Writer role by default.

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

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

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 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 Go outside of GCP, you must supply your GCP project ID and appropriate service account credentials directly to the Stackdriver Logging library for Go. This applies to running the agent on your own workstation, on your data centers' 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.

Was this page helpful? Let us know how we did:

Send feedback about...

Stackdriver Logging