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!')
  • 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 either in the Stackdriver Logging UI, or 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://googlecloudplatform.github.io/gcloud-node/#/docs/google-cloud/latest/guides/authentication
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!'

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