การบันทึกเป็นเครื่องมือสำคัญสำหรับการดีบักและติดตามโค้ด Cloud Functions ให้คุณมีตัวเลือกในการใช้ SDK ตัวบันทึกสำหรับ Node.js หรือ Python หรือมาตรฐานออบเจ็ก console
สำหรับการพัฒนาสำหรับเว็บ
Cloud Logging เป็นบริการที่คิดค่าบริการ คุณอาจถูกเรียกเก็บเงินหากคุณเกินโควต้าที่ไม่มีค่าใช้จ่าย สำหรับข้อมูลเพิ่มเติม โปรดดู ราคา Cloud Logging
การเขียนบันทึก
การใช้ SDK ตัวบันทึกฟังก์ชันคลาวด์
SDK ตัวบันทึกฟังก์ชันคลาวด์มีอินเทอร์เฟซมาตรฐานเพื่อรายงานสถานะจากฟังก์ชันไปยัง Cloud Logging คุณสามารถใช้ SDK นี้เพื่อบันทึกเหตุการณ์ด้วย ข้อมูลที่มีโครงสร้าง ซึ่งช่วยให้วิเคราะห์และติดตามได้ง่ายขึ้น
นำเข้าจากแพ็คเกจ logger
:
โหนด js
// All available logging functions
const {
log,
info,
debug,
warn,
error,
write,
} = require("firebase-functions/logger");
หลาม
from firebase_functions import logger
คำสั่ง
logger.log()
มีระดับบันทึก ข้อมูลคำสั่ง
logger.info()
มีระดับบันทึก INFOคำสั่ง
logger.warn()
มีระดับ บันทึกคำเตือนคำสั่ง
logger.error()
มีระดับบันทึก ข้อผิดพลาดคำสั่ง
logger.debug()
มีระดับบันทึก DEBUGข้อความระบบภายในมีระดับบันทึก DEBUG
ตัวอย่างนี้แสดงให้เห็นถึงฟังก์ชันการเขียนบันทึกพื้นฐาน:
โหนด js
exports.helloWorld = onRequest((request, response) => {
// sends a log to Cloud Logging
log("Hello logs!");
response.send("Hello from Firebase!");
});
หลาม
@https_fn.on_request()
def hello_world(req: https_fn.Request) -> https_fn.Response:
# sends a log to Cloud Logging
logger.log("Hello logs!")
return https_fn.Response("Hello from Firebase!")
ใช้ระดับบันทึกที่แตกต่างกันสำหรับบันทึกประเภทต่างๆ ในโค้ดฟังก์ชันของคุณ ข้อมูลที่มีโครงสร้างสามารถแนบไปกับบันทึกเป็นอาร์กิวเมนต์สุดท้ายได้ นี่คือตัวอย่างวิธีที่ฟังก์ชันสามารถใช้บันทึกแต่ละประเภท:
โหนด js
exports.getInspirationalQuote = onRequest(async (request, response) => {
const db = getFirestore();
const today = new Date();
const quoteOfTheMonthRef = db
.collection("quotes")
.doc(`${today.getFullYear()}`)
.collection("months")
.doc(`${today.getMonth()}`);
const DEFAULT_QUOTE =
"You miss 100% of the shots you don't take. -Wayne Gretzky";
let quote;
try {
const quoteOfTheMonthDocSnap = await quoteOfTheMonthRef.get();
// Attach relevant debugging information with debug()
debug("Monthly quote fetch result", {
docRef: quoteOfTheMonthRef.path,
exists: quoteOfTheMonthDocSnap.exists,
createTime: quoteOfTheMonthDocSnap.createTime,
});
if (quoteOfTheMonthDocSnap.exists) {
quote = quoteOfTheMonthDocSnap.data().text;
} else {
// Use warn() for lower-severity issues than error()
warn("Quote not found for month, sending default instead", {
docRef: quoteOfTheMonthRef.path,
dateRequested: today.toLocaleDateString("en-US"),
});
quote = DEFAULT_QUOTE;
}
} catch (err) {
// Attach an error object as the second argument
error("Unable to read quote from Firestore, sending default instead",
err);
quote = DEFAULT_QUOTE;
}
// Attach relevant structured data to any log
info("Sending a quote!", {quote: quote});
response.json({inspirationalQuote: quote});
});
หลาม
@https_fn.on_request()
def get_inspirational_quote(req: https_fn.Request) -> https_fn.Response:
firestore_client = firestore.client()
today = datetime.date.today()
quote_of_the_month_ref = (firestore_client.collection("quotes").doc(str(
today.year)).collection("months").doc(str(today.month)))
default_quote = "Python has been an important part of Google since the beginning, and remains so as the system grows and evolves."
quote = None
try:
quote_of_the_month = quote_of_the_month_ref.get()
# Attach relevant debugging information with debug()
logger.debug(
"Monthly quote fetch result",
docRef=quote_of_the_month.path,
exists=quote_of_the_month.exists,
createTime=quote_of_the_month.createTime,
)
if quote_of_the_month.exists:
quote = quote_of_the_month.to_dict()["text"]
else:
# Use warn() for lower-severity issues than error()
logger.warn(
"Quote not found for month, sending default instead",
doc_reference=quote_of_the_month.path,
date_requested=today.strftime("%Y-%m-%d"),
)
quote = default_quote
except:
e = sys.exc_info()[0]
# Attach an error object as the second argument
logger.error("Unable to read quote from Firestore, sending default instead", error=e)
quote = default_quote
# Attach relevant structured data to any log
logger.info("Sending a quote!", quote=quote)
return https_fn.Response("Hello from Firebase!")
ด้วย logger.write()
คุณสามารถเขียนรายการบันทึกที่มีระดับความรุนแรงของบันทึกเพิ่มเติม ได้แก่ CRITICAL
, ALERT
และ EMERGENCY
ดู LogSeverity
โหนด js
exports.appHasARegression = onRegressionAlertPublished((event) => {
write({
// write() lets you set additional severity levels
// beyond the built-in logger functions
severity: "EMERGENCY",
message: "Regression in production app",
issue: event.data.payload.issue,
lastOccurred: event.data.payload.resolveTime,
});
});
หลาม
@crashlytics_fn.on_regression_alert_published()
def app_has_regression(alert: crashlytics_fn.CrashlyticsRegressionAlertEvent) -> None:
logger.write(
severity="EMERGENCY",
message="Regression in production app",
issue=alert.data.payload.issue,
last_occurred=alert.data.payload.resolve_time,
)
print(alert)
การใช้ 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()
มีระดับบันทึก ข้อมูล - คำสั่ง
console.info()
มีระดับบันทึก ข้อมูล - คำสั่ง
console.warn()
มีระดับบันทึก ข้อผิดพลาด - คำสั่ง
console.error()
มีระดับบันทึก ข้อผิดพลาด - ข้อความระบบภายในมีระดับบันทึก DEBUG
กำลังดูบันทึก
บันทึกสำหรับฟังก์ชันคลาวด์สามารถดูได้ใน คอนโซล 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
การใช้ UI การบันทึกบนคลาวด์
คุณสามารถ ดูบันทึกสำหรับฟังก์ชันคลาวด์ได้ ใน Cloud Logging UI
กำลังวิเคราะห์บันทึก
Cloud Logging นำเสนอชุดเครื่องมือวิเคราะห์บันทึกอันทรงพลังที่คุณสามารถใช้ตรวจสอบฟังก์ชันคลาวด์ของคุณได้
แผนภูมิและการแจ้งเตือน
เมื่อคุณสร้างตัวชี้วัดตามบันทึกเพื่อตรวจสอบการทำงานของคุณแล้ว คุณสามารถสร้างแผนภูมิและการแจ้งเตือนตามตัวชี้วัดเหล่านี้ได้ ตัวอย่างเช่น คุณสามารถสร้างแผนภูมิเพื่อแสดงภาพเวลาแฝงเมื่อเวลาผ่านไป หรือสร้างการแจ้งเตือนเพื่อแจ้งให้คุณทราบว่ามีข้อผิดพลาดเกิดขึ้นบ่อยเกินไปหรือไม่
ดู การสร้างแผนภูมิและการแจ้งเตือน สำหรับข้อมูลโดยละเอียดเกี่ยวกับวิธีการใช้ตัววัดตามบันทึกในแผนภูมิและนโยบายการแจ้งเตือน