Google is committed to advancing racial equity for Black communities. See how.

Write and view logs

Logging is an important tool for debugging and monitoring code. Cloud Functions gives you the option of using its logger SDK, custom Google Cloud Logging, or the console object standard for developing for the web.

Writing logs

While the Cloud Functions logger SDK is recommended for most situations, you might choose one of the other options for these reasons:

  • You have an existing code base and prefer not to refactor from console.log.
  • You're familiar with Cloud Logging (formerly StackDriver logging) and prefer to use it for custom logging.

Using the Cloud Functions logger SDK

The Cloud Functions logger SDK provides a standard interface that has a similar api to console.log statements and supports other log levels. You can use this SDK to log events with structured data, enabling easier analysis and monitoring.

The logger SDK supports log entries as part of a wildcard import. For example:

  const functions = require("firebase-functions");

  functions.logger.log("Hello from info. Here's an object:", someObj);

Alternatively, you can use individual exports. This example demonstrates structured data attached to the log as the last argument:

const { warn } = require("firebase-functions/lib/logger");


// Attach structured data to the log as the last argument.
warn("This is a 'WARNING' severity message with some metadata.", {
  key1: 'val1',
  key2: 'val2'
});
  • logger.log() commands have the INFO log level.
  • logger.info() commands have the INFO log level.
  • logger.warn() commands have the ERROR log level.
  • logger.error() commands have the ERROR log level.
  • Internal system messages have the DEBUG log level.

With logger.write(), you can write log entries addition log severity levels of CRITICAL, ALERT, and EMERGENCY. See LogSeverity.

Custom Cloud Logging logs

Cloud Functions logs with the logger SDK are backed by Cloud Logging. You can use the StackDriver logging library for Node.js to log events with structured data, enabling easier analysis and monitoring.

const { Logging } = require('@google-cloud/logging');

// ...

// Instantiate the StackDriver Logging SDK. The project ID will
// be automatically inferred from the Cloud Functions environment.
const logging = new Logging();
const log = logging.log('my-custom-log-name');

// This metadata is attached to each log entry. This specifies a fake
// Cloud Function called 'Custom Metrics' in order to make your custom
// log entries appear in the Cloud Functions logs viewer.
const METADATA = {
  resource: {
    type: 'cloud_function',
    labels: {
      function_name: 'CustomMetrics',
      region: 'us-central1'
    }
  }
};

// ...

// Data to write to the log. This can be a JSON object with any properties
// of the event you want to record.
const data = {
  event: 'my-event',
  value: 'foo-bar-baz',

  // Optional 'message' property will show up in the Firebase
  // console and other human-readable logging surfaces
  message: 'my-event: foo-bar-baz'
};

// Write to the log. The log.write() call returns a Promise if you want to
// make sure that the log was written successfully.
const entry = log.entry(METADATA, data);
log.write(entry);

Using console.log

To emit a log line from your function, you can use standard JavaScript logging calls such as console.log and console.error:

exports.helloError = functions.https.onRequest((request, response) => {
  console.log('I am a log entry!');
  response.send('Hello World...');
});
  • console.log() commands have the INFO log level.
  • console.info() commands have the INFO log level.
  • console.warn() commands have the ERROR log level.
  • console.error() commands have the ERROR log level.
  • Internal system messages have the DEBUG log level.

With console.log, you may notice issues in the readability of your logs when migrating from the Node.js 8 runtime to Node.js 10 or 12. To work around this temporarily, add the following library to your functions:

require("firebase-functions/lib/logger/compat");

The recommended long-term solution for migrating to Node.js 10 or 12 is to refactor code to use the logger SDK.

Viewing logs

Logs for Cloud Functions are viewable either in the Firebase console, Cloud Logging UI, or via the firebase command-line tool.

Using the Firebase CLI

To view logs with the firebase tool, use the functions:log command:

firebase functions:log

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

firebase functions:log --only <FUNCTION_NAME>

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

firebase help functions:log

Using the Firebase console

You can view logs for Cloud Functions from the Firebase console.

Using the Cloud Logging UI

You can view logs for Cloud Functions in the Cloud Logging UI.

Analyzing logs

Cloud Logging offers a powerful suite of logs analysis tools that you can use to monitor your Cloud Functions. These tools are especially powerful when combined with custom logging.

Logs-based metrics

In the Cloud Logging UI, use the advanced filter field to narrow the log scope to the function you want to analyze, then click Submit Filter to filter the logs. For example, you could analyze only logs from a single function:

resource.type="cloud_function"
resource.labels.function_name="myCloudFunction"
resource.labels.region="us-central1"

If you logged events with custom JSON payloads, you can filter using the keys and values you provided:

resource.type="cloud_function"
resource.labels.function_name="CustomMetrics"
jsonPayload.event="my-event"

Once you have filtered the logs, you can analyze them using logs-based metrics. Click the Create Metric button to open the metric editor and select a metric type:

  • Counter metrics: count the number of log entries that match a filter. For example you could count the number of times a certain event occurs.
  • Distribution metrics: accumulate numeric data from log entries that match a filter. For example you could track the latencies of certain operations within your functions.

For text-based logs, such as those resulting from console.log() in your functions, you can extract values and labels from the textPayload field using regular expressions. For custom logs with structured data you can directly access the data in the jsonPayload field.

Charts and alerts

Once you have created logs-based metrics to monitor your functions, you can create charts and alerts based on these metrics. For example, you could create a chart to visualize latency over time, or create an alert to let you know if a certain error occurs too often.

See Creating Charts and Alerts for detailed information on how to use logs-based metrics in charts and alerting policies.