Logging 是程式碼偵錯及監控的重要工具。Cloud Functions 可讓您選擇使用其記錄器 SDK、自訂 Google Cloud Logging,或 console
物件標準來進行網頁開發。
寫入記錄
雖然大多數情況下都建議使用 Cloud Functions 記錄器 SDK,但您可能會基於下列原因選擇其他選項:
- 已有現有的程式碼集,且不想重構
console.log
。 - 您已熟悉 Cloud Logging (舊稱 StackDriver 記錄),且希望使用 Cloud Logging 進行自訂記錄。
使用 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()
即可寫入記錄項目,加上 CRITICAL
、ALERT
和 EMERGENCY
的記錄嚴重性等級。請參閱 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
如要從函式記錄,建議您針對平台使用記錄器 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 記錄層級。
查看記錄
您可在 Google Cloud 控制台、Cloud Logging UI 或 firebase
指令列工具中查看 Cloud Functions 的記錄檔。
使用 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 Logging UI 中查看 Cloud Functions 的記錄檔。
分析記錄檔
Cloud Logging 提供一套功能強大的記錄檔分析工具,可用來監控 Cloud Functions。
圖表和快訊
建立記錄指標來監控函式後,您就可以根據這些指標建立圖表與快訊。例如,您可以建立圖表,以視覺化方式呈現一段時間內的延遲時間,或是建立快訊通知您特定錯誤發生的頻率過高。
如要進一步瞭解如何在圖表和快訊政策中使用記錄指標,請參閱建立圖表與快訊。