Writing, Viewing, and Responding to Logs

Writing logs

To emit a log line from your Cloud Function, write to standard output or standard error.


Use console.log() and console.error():
exports.helloWorld = (req, res) => {
  console.log('I am a log entry!');
  console.error('I am an error!');

Python (Beta)

For example, use print() or the logging module:
import logging

def hello_world(data, context):
    """Background Cloud Function.
         data (dict): The dictionary with data specific to the given event.
         context (google.cloud.functions.Context): The event metadata.
    print('Hello, stdout!')
    logging.warn('Hello, logging handler!')

Go (Beta)

// Package helloworld provides a set of Cloud Function samples.
package helloworld

import (

// Loggers for printing to Stdout and Stderr.
var (
	stdLogger = log.New(os.Stdout, "", 0)
	logger    = log.New(os.Stderr, "", 0)

// HelloLogging logs messages.
func HelloLogging(w http.ResponseWriter, r *http.Request) {
	stdLogger.Println("I am a log entry!")
	logger.Println("I am an error!")
  • Logs to standard output have the INFO log level.
  • Logs to standard error have the ERROR log level.
  • Internal system messages have the DEBUG log level.

Viewing logs

Using the command-line tool

Logs for Cloud Functions are viewable in the Stackdriver Logging UI, and via the gcloud command-line tool.

To view logs with the gcloud tool, use the logs read command:

gcloud functions logs read

To view the logs for a specific function, provide the function name as an argument:

gcloud functions logs read FUNCTION_NAME

You can even view the logs for a specific execution:

gcloud functions logs read FUNCTION_NAME --execution-id EXECUTION_ID

For the full range of log viewing options, view the help for logs read:

gcloud functions logs read -h

Using the Logging dashboard

You can also view logs for Cloud Functions from the GCP Console.

Using the Logging API

Logs can also be written and retrieved through the Logging API. The Stackdriver Logging Client Libraries provide an idiomatic interface to the Logging API:


For more information, see the Node.js Client Library reference.
// By default, the client will authenticate using the service account file
// specified by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use
// the project specified by the GCLOUD_PROJECT environment variable. See
// https://github.com/googleapis/google-cloud-node/blob/master/docs/authentication.md
const Logging = require('@google-cloud/logging');

function getLogEntries() {
  // Instantiates a client
  const logging = Logging();

  const options = {
    pageSize: 10,
    filter: 'resource.type="cloud_function"',

  // Retrieve the latest Cloud Function log entries
  // See https://googlecloudplatform.github.io/gcloud-node/#/docs/logging
  return logging.getEntries(options).then(([entries]) => {
    entries.forEach(entry => console.log(entry));
    return entries;

Python (Beta)

For more information, see the Python Client Library reference.
import os
import google.cloud.logging as cloud_logging
cloud_client = cloud_logging.Client()
log_name = 'cloudfunctions.googleapis.com%2Fcloud-functions'
cloud_logger = cloud_client.logger(log_name.format(os.getenv('GCP_PROJECT')))

def get_log_entries(request):
    HTTP Cloud Function that displays log entries from Cloud Functions.
        request (flask.Request): The request object.
        The response text, or any set of values that can be turned into a
        Response object using `make_response`

    all_entries = cloud_logger.list_entries(page_size=10)
    entries = next(all_entries.pages)

    for entry in entries:
        timestamp = entry.timestamp.isoformat()
        print('* {}: {}'.format
              (timestamp, entry.payload))

    return 'Done!'

Go (Beta)

For more information, see the Go Client Library reference.

// Package log contains logging examples.
package log

import (

	loggingpb "google.golang.org/genproto/googleapis/logging/v2"

// logEntries retrieves log entries from projectID and writes them to the
// passed io.Writer.
func logEntries(w io.Writer, projectID string) error {
	ctx := context.Background()
	client, err := logging.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("logging.NewClient: %v", err)

	req := &loggingpb.ListLogEntriesRequest{
		ResourceNames: []string{"projects/" + projectID},
		PageSize:      10,

	fmt.Fprintln(w, "Entries:")
	it := client.ListLogEntries(ctx, req)
	// Wrap in a for loop to get all available log entries.
	resp, err := it.Next()
	if err == iterator.Done {
		return nil
	if err != nil {
		return fmt.Errorf("it.Next: %v", err)
	fmt.Fprintln(w, resp.GetPayload())
	return nil

Responding to logs

You can respond to Stackdriver-logged events by forwarding their logs to a Cloud Function. For more information, see the Second-Party Triggers with Stackdriver page.

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

Send feedback about...

Cloud Functions Documentation