寫入和檢視記錄


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

寫入記錄

Cloud Functions 記錄器 適用於大多數情況 您可能會基於下列原因選擇其他選項:

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

使用 Cloud Functions 記錄器 SDK

Cloud Functions Logger SDK 提供標準介面,其中包含 類似 console.log 陳述式的類似 API,並支援其他記錄層級 您可以使用這個 SDK 搭配結構化資料記錄事件。 簡化分析及監控作業

Logger 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(),您可以編寫記錄項目,並新增 CRITICALALERTEMERGENCY 的記錄嚴重性層級。請參閱「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

如果從函式進行記錄,建議您使用 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 記錄層級。

查看記錄

如要查看「Cloud Functions」的記錄,請前往 Google Cloud 控制台Cloud Logging UI,或透過 firebase 指令列工具。

使用 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 Functions」的記錄Cloud Logging 使用者介面中顯示。

分析記錄檔

Cloud Logging 提供強大的記錄分析工具套件, 監控Cloud Functions

圖表與快訊

建立記錄指標來監控函式後, 並根據這些指標建立圖表和快訊。舉例來說 以圖表呈現一段時間內的延遲時間,也可以建立快訊以通知您 避免發生特定錯誤太常發生

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