寫入及查看記錄 (第 1 代)

記錄是偵錯和監控程式碼的重要工具。Cloud Functions 可讓您選擇使用記錄器 SDK、自訂 Google Cloud Logging,或網頁開發標準 console 物件。

寫入記錄

雖然在大多數情況下,建議使用 Cloud Functions logger SDK,但您可能會基於下列原因選擇其他選項:

  • 您已有程式碼集,不想從 console.log 重構。
  • 您熟悉 Cloud Logging (原稱 StackDriver Logging),且偏好使用這項服務進行自訂記錄。

使用 Cloud Functions 記錄器 SDK

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

記錄器 SDK 支援記錄項目,做為萬用字元匯入的一部分。例如:

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

  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() 時,您可以寫入記錄嚴重性等級為 CRITICAL、ALERT 和 EMERGENCY 的記錄項目。請參閱 LogSeverity。

自訂 Cloud Logging 記錄

使用記錄器 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

如要從函式記錄,建議使用平台的記錄器 SDK。使用 Node.js 時,您可以改用標準 JavaScript 記錄呼叫,例如 console.log 和 console.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。

圖表和快訊

建立記錄指標來監控函式後,即可根據這些指標建立圖表和警告。舉例來說,您可以建立圖表,以視覺化方式呈現延遲時間,或建立快訊,在特定錯誤發生頻率過高時通知您。

如要進一步瞭解如何在圖表和警告政策中使用記錄指標,請參閱「建立圖表和警告」。