寫入和檢視記錄


Logging 是偵錯及監控程式碼的重要工具。Cloud Functions 可讓您選擇使用其 Logger SDK、自訂的 Google Cloud Logging,或 console 物件標準進行網路開發。

寫入記錄

雖然大部分情況下都建議使用 Cloud Functions 記錄器 SDK,但基於以下原因,您可能會選擇其他選項:

  • 您有現有程式碼集,且不想從 console.log 重構。
  • 您熟悉 Cloud Logging (前身為 StackDriver 記錄功能),且偏好將其用於自訂記錄。

使用 Cloud Functions 記錄器 SDK

Cloud Functions Logger SDK 提供標準介面,此介面與 console.log 陳述式類似的 API,並支援其他記錄層級。您可以使用這個 SDK 記錄結構化資料的事件,更輕鬆地分析及監控資料。

Logger SDK 支援使用萬用字元匯入功能。例如:

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

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

此外,您也可以使用個別匯出項目。這個範例說明附加至記錄檔的結構化資料做為最後一個引數:

const { warn } = require("firebase-functions/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() 指令擁有 INFO 記錄層級。
  • logger.info() 指令擁有 INFO 記錄層級。
  • logger.warn() 指令擁有 WARNING 記錄層級。
  • logger.error() 指令擁有 ERROR 記錄層級。
  • 內部系統訊息擁有 DEBUG 記錄層級。

使用 logger.write() 時,您可以寫入 CRITICALALERTEMERGENCY 的記錄嚴重性等級。請參閱 LogSeverity 一文。

自訂 Cloud Logging 記錄檔

使用 Logger SDK 的 Cloud Functions 記錄由 Cloud Logging 支援。您可以使用 Node.js 適用的 Cloud Logging 程式庫,以結構化資料記錄事件,輕鬆進行分析及監控。

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

// ...

// Instantiate the 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);

正在使用 console.log

從函式進行記錄的建議解決方案,是使用您平台的 Logger SDK。透過 Node.js,您可以改用標準 JavaScript 記錄呼叫,例如 console.logconsole.error,但首先需要用一個特殊模組來修補標準方法,才能正常運作:

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

取得記錄器相容性模組後,您可以照常在程式碼中使用 console.log() 方法:

exports.helloError = functions.https.onRequest((request, response) => {
  console.log('I am a log entry!');
  response.send('Hello World...');
});
  • console.log() 指令擁有 INFO 記錄層級。
  • console.info() 指令擁有 INFO 記錄層級。
  • console.warn() 指令擁有 ERROR 記錄層級。
  • console.error() 指令擁有 ERROR 記錄層級。
  • 內部系統訊息擁有 DEBUG 記錄層級。

查看記錄

您可以透過 Google Cloud 控制台、Cloud Logging UI 或 firebase 指令列工具查看 Cloud Functions 的記錄。

使用 Firebase CLI

如要使用 firebase 工具查看記錄,請使用 functions:log 指令:

firebase functions:log

如要查看特定函式的記錄,請提供函式名稱做為引數:

firebase functions:log --only <FUNCTION_NAME>

如需完整範圍的記錄檢視選項,請查看 functions:log 的說明:

firebase help functions:log

使用 Google Cloud 控制台

您可以在 Google Cloud 控制台中查看函式的記錄。

使用 Cloud Logging UI

您可以在 Cloud Logging UI 中查看 Cloud Functions 的記錄

分析記錄檔

Cloud Logging 提供一套強大的記錄分析工具,可用於監控 Cloud Functions。

圖表與快訊

建立記錄指標來監控函式後,您可以根據這些指標建立圖表和快訊。例如,您可以建立圖表,以視覺化方式呈現一段時間內的延遲時間,或是建立快訊以通知您特定錯誤是否過於頻繁。

如要進一步瞭解如何在圖表和快訊政策中使用記錄指標,請參閱建立圖表與快訊