日誌記錄是調試和監控程式碼的重要工具。 Cloud Functions 讓您可以選擇使用其記錄器 SDK、自訂 Google Cloud Logging 或console
標準來進行 Web 開發。
寫入日誌
雖然在大多數情況下建議使用 Cloud Functions記錄器SDK,但基於以下原因,您也可以選擇其他選項之一:
- 您擁有現有的程式碼庫,也不希望從
console.log
進行重構。 - 您熟悉 Cloud Logging(以前稱為 StackDriver 日誌記錄)並且更喜歡將其用於自訂日誌記錄。
使用 Cloud Functions 記錄器 SDK
Cloud Functions logger SDK 提供了一個標準接口,該接口具有與console.log
語句類似的 api,並支援其他日誌等級。您可以使用此 SDK 來記錄具有結構化資料的事件,從而更輕鬆地進行分析和監控。
記錄器 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()
,您可以寫入日誌條目以及CRITICAL
、 ALERT
和EMERGENCY
日誌嚴重性等級。請參閱LogSeverity 。
自訂雲日誌記錄
帶有記錄器 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日誌等級。
查看日誌
Cloud Functions 的日誌可以在Google Cloud Console 、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 Console
您可以在Google Cloud Console中查看函數日誌。
使用 Cloud Logging UI
您可以在 Cloud Logging UI 中查看 Cloud Functions 日誌。
分析日誌
Cloud Logging 提供了一套強大的日誌分析工具,您可以使用它們來監控您的 Cloud Functions。
圖表和警報
建立基於日誌的指標來監控功能後,您可以根據這些指標建立圖表和警報。例如,您可以建立一個圖表來視覺化一段時間內的延遲,或建立警報來讓您知道某個錯誤是否經常發生。
有關如何在圖表和警報策略中使用基於日誌的指標的詳細信息,請參閱建立圖表和警報。