寫入和檢視記錄


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

寫入記錄

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

  • 您有現有程式碼集,且不想從 console.log 重構。
  • 您熟悉 Cloud Logging (原稱 StackDriver 記錄功能),且需要 以用於自訂記錄

使用 Cloud Functions 記錄器 SDK

Cloud Functions 記錄器 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 記錄層級。

查看記錄

如要查看 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 UI 中

分析記錄檔

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

圖表與快訊

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

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