Setting Up Cloud Logging for Go

We provide a standard library-compatible logging package. TheCloud Logging library for Go exposes a higher-level layer for working withCloud Logging.

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

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.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.create permission.Learn how to grant roles.
    Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Cloud Logging API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enable permission.Learn how to grant roles.

    Enable the API

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.create permission.Learn how to grant roles.
    Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.

    Go to project selector

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Cloud Logging API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enable permission.Learn how to grant roles.

    Enable the API

Installing the package

go get cloud.google.com/go/logging

Using the Go logger

// Sample stdlogging writes log.Logger logs to the Cloud Logging.packagemainimport("context""log""cloud.google.com/go/logging")funcmain(){ctx:=context.Background()// Sets your Google Cloud Platform project ID.projectID:="YOUR_PROJECT_ID"// Creates a client.client,err:=logging.NewClient(ctx,projectID)iferr!=nil{log.Fatalf("Failed to create client: %v",err)}deferclient.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// Cloud Logs.logger.Println("hello world")}

For more information on installation, see thedocumentation for theCloud Logging library for Go. You can also report issues using theissue tracker.

Write logs with the Cloud Logging client library

For information on using the Cloud Logging client library forGo directly, seeCloud Logging Client Libraries.

Run on Google Cloud

For an application to write logs by using the Cloud Logging library for Go, theservice account for the underlying resource must have theLogs Writer (roles/logging.logWriter)IAM role.Most Google Cloud environments automatically configure the defaultservice account to have this role.

App Engine

Cloud Logging is automatically enabled for App Engine, and your app'sdefault service account hasthe IAM permissions by default to write log entries.

For more information, seeWriting and viewinglogs.

Google Kubernetes Engine (GKE)

GKE automatically grants the default service accounttheLogs Writer (roles/logging.logWriter) IAM role.If you useWorkload Identity Federation for GKEwith this default service account to let workloads access specificGoogle Cloud APIs, then no additional configuration is required.However, if you use Workload Identity Federation for GKE with acustom IAM service account, then ensure that the custom serviceaccount has the role of Logs Writer (roles/logging.logWriter).

If needed, you can also use the following command to add thelogging.writeaccess scope when creating the cluster:

gcloudcontainerclusterscreateexample-cluster-name\--scopeshttps://www.googleapis.com/auth/logging.write

Compute Engine

Note: To use the Cloud Logging library for Go on aCompute Engine VM instance,you do not need to install the Cloud Logging agent.

When usingCompute Engine VM instances, add thecloud-platformaccess scope to each instance. When creating a new instance through theGoogle Cloud console, you can do this in theIdentity and API access sectionof theCreate Instance panel. Use the Compute Engine default serviceaccount or another service account of your choice, and selectAllow full access to all Cloud APIs in theIdentity and API accesssection. Whichever serviceaccount you select, ensure that it has been granted theLogs Writer role in theIAM & Admin section of theGoogle Cloud console.

Run locally and elsewhere

To use the Cloud Logging library for Go outside of Google Cloud,including running the library on your own workstation, on your data center'scomputers, or on the VM instances of another cloud provider, you mustset up Application Default Credentials (ADC) in your local environment toauthenticate to the Cloud Logging library for Go.

For more information, seeSet up ADC for on-premises or another cloud provider.

View the logs

In the Google Cloud console, go to theLogs Explorer page:

Go toLogs Explorer

If you use the search bar to find this page, then select the result whose subheading isLogging.

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

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

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

  • If you are deploying your application on Google Kubernetes Engine, yourcluster's logging configuration determines the resource type of the logentries. For a detaileddiscussion on the Legacy Google Cloud Observability and the Google Cloud ObservabilityKubernetes Monitoring solutions, and how those options affect theresource type, seeMigrating to Google Cloud Observability 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, clickmenu (▾)at the top of the Logs Explorer andthen selectConvert to advanced filter.
    2. Clear the content that appears in the filter box.
    3. ClickSubmit Filter.

    You can examine the individual entries to identify your resources.

For additional information, seeUsing the Logs Explorer.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-15 UTC.