日誌記錄是調試和監控代碼的重要工具。 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 。您可以改為使用標準 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。
圖表和警報
創建基於日誌的指標來監控功能後,您可以根據這些指標創建圖表和警報。例如,您可以創建一個圖表來可視化一段時間內的延遲,或者創建一個警報來讓您知道某個錯誤是否經常發生。
有關如何在圖表和警報策略中使用基於日誌的指標的詳細信息,請參閱創建圖表和警報。