कोड को डीबग और मॉनिटर करने के लिए, लॉग करना एक अहम टूल है.
Cloud Functions आपको यह विकल्प देता है कि लॉगर SDK टूल
Node.js
या Python,
या वेब के लिए डेवलप करने के लिए console
ऑब्जेक्ट स्टैंडर्ड का इस्तेमाल करें.
क्लाउड लॉगिंग एक ऐसी सेवा है जिसके लिए शुल्क देना पड़ता है; तो आपसे बिल लिया जा सकता है अगर नो-कॉस्ट ऑप्शन से ज़्यादा हो. ज़्यादा जानकारी के लिए, यह देखें Cloud Logging की कीमत.
लेखन लॉग
Cloud Functions लॉगर SDK टूल का इस्तेमाल करना
Cloud Functions लॉगर SDK टूल एक स्टैंडर्ड इंटरफ़ेस उपलब्ध कराता है का इस्तेमाल करें. इस SDK टूल का इस्तेमाल करके, स्ट्रक्चर्ड डेटा, ताकि वे आसानी से विश्लेषण और निगरानी कर सकें.
logger
सबपैकेज से इंपोर्ट करें:
Node.js
// All available logging functions
const {
log,
info,
debug,
warn,
error,
write,
} = require("firebase-functions/logger");
Python
from firebase_functions import logger
logger.log()
निर्देशों में INFO लॉग लेवल होता है.logger.info()
निर्देशों में INFO लॉग लेवल होता है.logger.warn()
निर्देशों में चेतावनी का लॉग लेवल होता है.logger.error()
निर्देशों में गड़बड़ी का लॉग लेवल होता है.logger.debug()
निर्देशों में डीबग लॉग लेवल होता है.इंटरनल सिस्टम मैसेज में डीबग लॉग लेवल होता है.
यह उदाहरण, बेसिक लॉग लिखने वाले फ़ंक्शन के बारे में बताता है:
Node.js
exports.helloWorld = onRequest((request, response) => {
// sends a log to Cloud Logging
log("Hello logs!");
response.send("Hello from Firebase!");
});
Python
@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!")
अपने फ़ंक्शन कोड में अलग-अलग तरह के लॉग इन के लिए, अलग-अलग लॉग लेवल का इस्तेमाल करें. स्ट्रक्चर्ड डेटा को आखिरी तर्क के तौर पर लॉग में अटैच किया जा सकता है. यहाँ है फ़ंक्शन, हर लॉग टाइप का इस्तेमाल कैसे कर सकता है, इसका उदाहरण:
Node.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});
});
Python
@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 देखें.
Node.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,
});
});
Python
@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");
जब आपके लिए Logger के साथ काम करने वाला मॉड्यूल ज़रूरी हो, तो
आपके कोड में सामान्य रूप से 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()
निर्देशों में गड़बड़ी का लॉग लेवल होता है.console.error()
निर्देशों में गड़बड़ी का लॉग लेवल होता है.- इंटरनल सिस्टम मैसेज में डीबग लॉग लेवल होता है.
लॉग देखना
Cloud Functions के लॉग या तो
Google Cloud Console,
Cloud Logging के यूज़र इंटरफ़ेस (यूआई) का इस्तेमाल करें या 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 Functions के लिए लॉग देखे जा सकते हैं को भी हटा सकते हैं.
लॉग का विश्लेषण किया जा रहा है
क्लाउड में लॉग करने की सुविधा, लॉग विश्लेषण टूल का बेहतरीन सुइट उपलब्ध कराती है. इसकी मदद से ये काम किए जा सकते हैं का इस्तेमाल अपने Cloud Functions को मॉनिटर करने के लिए करें.
चार्ट और सूचनाएं
अपने फ़ंक्शन को मॉनिटर करने के लिए, लॉग पर आधारित मेट्रिक बनाने के बाद, ये काम किए जा सकते हैं इन मेट्रिक के आधार पर चार्ट और अलर्ट बना सकते हैं. उदाहरण के लिए, आप समय के साथ इंतज़ार के समय को दिखाने के लिए एक चार्ट या आपको इसकी जानकारी देने के लिए सूचना तैयार करें अगर कोई खास गड़बड़ी बार-बार होती है.
इसके बारे में ज़्यादा जानकारी के लिए चार्ट और अलर्ट बनाना देखें का इस्तेमाल करें.