日誌記錄是調試和監控代碼的重要工具。 Cloud Functions 讓您可以選擇使用其記錄器 SDK、自定義 Google Cloud Logging 或用於 Web 開發的console
對象標準。
寫日誌
雖然在大多數情況下建議使用 Cloud Functions記錄器SDK,但您可能會出於以下原因選擇其他選項之一:
- 您有一個現有的代碼庫並且不想從
console.log
重構。 - 您熟悉 Cloud Logging(以前稱為 StackDriver 日誌記錄)並且更喜歡將其用於自定義日誌記錄。
使用 Cloud Functions 記錄器 SDK
Cloud Functions 記錄器 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
。請參閱日誌嚴重性。
自定義 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 。您可以改為使用標準的 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中查看函數日誌。
使用 Cloud Logging 界面
您可以在 Cloud Logging UI 中查看 Cloud Functions 的日誌。
分析日誌
Cloud Logging 提供了一套功能強大的日誌分析工具,您可以使用它們來監控您的 Cloud Functions。這些工具在與自定義日誌記錄結合使用時特別強大。
基於日誌的指標
在 Cloud Logging UI 中,使用高級過濾器字段將日誌範圍縮小到您要分析的功能,然後單擊提交過濾器以過濾日誌。例如,您可以僅分析來自單個函數的日誌:
resource.type="cloud_function"
resource.labels.function_name="myCloudFunction"
resource.labels.region="us-central1"
如果您使用自定義 JSON 負載記錄事件,則可以使用您提供的鍵和值進行過濾:
resource.type="cloud_function"
resource.labels.function_name="CustomMetrics"
jsonPayload.event="my-event"
過濾日誌後,您可以使用基於日誌的指標對其進行分析。單擊創建指標按鈕以打開指標編輯器並選擇指標類型:
- 計數器指標:計算與過濾器匹配的日誌條目數。例如,您可以計算某個事件發生的次數。
- 分佈指標:從匹配過濾器的日誌條目中累積數字數據。例如,您可以跟踪函數中某些操作的延遲。
對於基於文本的日誌,例如函數中的console.log()
生成的日誌,您可以使用正則表達式從textPayload
字段中提取值和標籤。對於具有結構化數據的自定義日誌,您可以直接訪問jsonPayload
字段中的數據。
圖表和警報
一旦您創建了基於日誌的指標來監控您的功能,您就可以根據這些指標創建圖表和警報。例如,您可以創建一個圖表來可視化隨時間變化的延遲,或者創建一個警報來讓您知道某個錯誤是否經常發生。
有關如何在圖表和警報策略中使用基於日誌的指標的詳細信息,請參閱創建圖表和警報。