如何让日志在 Cloud Functions for Firebase 中显示为错误
How to get logs to show up as errors in Cloud Functions for Firebase
当我的云函数发生错误时,它不会显示在 firebase 控制台中或记录为错误。
我希望错误显示在 Firebase 控制台功能页面的运行状况选项卡上,以便我知道该错误。
目前它仅作为正常日志条目显示在功能日志中(见图)。
如何调整我的代码,使云函数中出现错误,以便我可以看到它。
请注意,此函数由 Stripe 的 webhook 调用。
export const stripeWHEInvoiceCreated = functions
.region('us-east1')
.https.onRequest(async (req, res) => {
try {
const sig = req.headers['stripe-signature'];
const event = stripe.webhooks.constructEvent(
(req as any).rawBody,
sig,
webhookSecretInvoiceCreated
);
await internalInvoiceCreated(event);
res.sendStatus(200);
} catch (err) {
res.status(400).send(err);
}
});
export const internalInvoiceCreated = async(event: any) => {
try {
// Do stuff
} catch(err) {
console.log('An error occurred');
console.log(err);
throw err;
}
return;
}
最简单的方法是使用 console.error()
而不是 console.log()
。
如果您想以最好的方式进行日志记录,您应该使用函数 SDK 的日志记录功能,如 documentation.
中所示
const functions = require("firebase-functions");
functions.logger.error(...);
当我的云函数发生错误时,它不会显示在 firebase 控制台中或记录为错误。
我希望错误显示在 Firebase 控制台功能页面的运行状况选项卡上,以便我知道该错误。
目前它仅作为正常日志条目显示在功能日志中(见图)。
如何调整我的代码,使云函数中出现错误,以便我可以看到它。 请注意,此函数由 Stripe 的 webhook 调用。
export const stripeWHEInvoiceCreated = functions
.region('us-east1')
.https.onRequest(async (req, res) => {
try {
const sig = req.headers['stripe-signature'];
const event = stripe.webhooks.constructEvent(
(req as any).rawBody,
sig,
webhookSecretInvoiceCreated
);
await internalInvoiceCreated(event);
res.sendStatus(200);
} catch (err) {
res.status(400).send(err);
}
});
export const internalInvoiceCreated = async(event: any) => {
try {
// Do stuff
} catch(err) {
console.log('An error occurred');
console.log(err);
throw err;
}
return;
}
最简单的方法是使用 console.error()
而不是 console.log()
。
如果您想以最好的方式进行日志记录,您应该使用函数 SDK 的日志记录功能,如 documentation.
中所示const functions = require("firebase-functions");
functions.logger.error(...);