Write and view logs

Logging is an important tool for debugging and monitoring code. Cloud Functions gives you the same console object that you'd expect while developing for the web.

Writing logs

Simple logs

To emit a log line from your function, 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.

Custom StackDriver logs

Cloud Functions logs are backed by StackDriver 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);

Viewing logs

Logs for Cloud Functions are viewable either in the Firebase console, Stackdriver 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 Stackdriver logging UI

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

Analyzing logs

StackDriver 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 StackDriver 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:


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


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.