|
| 1 | +/** |
| 2 | + * Copyright 2023 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +const {onRequest} = require("firebase-functions/v2/https"); |
| 18 | +const { |
| 19 | + onRegressionAlertPublished, |
| 20 | +} = require("firebase-functions/v2/alerts/crashlytics"); |
| 21 | + |
| 22 | +// [START loggerImport] |
| 23 | +// All available logging functions |
| 24 | +const { |
| 25 | + log, |
| 26 | + info, |
| 27 | + debug, |
| 28 | + warn, |
| 29 | + error, |
| 30 | + write, |
| 31 | +} = require("firebase-functions/logger"); |
| 32 | +// [END loggerImport] |
| 33 | + |
| 34 | +const {initializeApp} = require("firebase-admin/app"); |
| 35 | +const {getFirestore} = require("firebase-admin/firestore"); |
| 36 | +// Initialize admin sdk for Firestore read in getInspirationalQuote |
| 37 | +initializeApp(); |
| 38 | + |
| 39 | +// [START helloLogs] |
| 40 | +exports.helloWorld = onRequest((request, response) => { |
| 41 | + // sends a log to Cloud Logging |
| 42 | + log("Hello logs!"); |
| 43 | + |
| 44 | + response.send("Hello from Firebase!"); |
| 45 | +}); |
| 46 | +// [END helloLogs] |
| 47 | + |
| 48 | +// [START logsKitchenSink] |
| 49 | +exports.getInspirationalQuote = onRequest(async (request, response) => { |
| 50 | + const db = getFirestore(); |
| 51 | + const today = new Date(); |
| 52 | + const quoteOfTheMonthRef = db |
| 53 | + .collection("quotes") |
| 54 | + .doc(today.getFullYear()) |
| 55 | + .collection("months") |
| 56 | + .doc(today.getMonth()); |
| 57 | + |
| 58 | + const DEFAULT_QUOTE = |
| 59 | + "You miss 100% of the shots you don't take. -Wayne Gretzky"; |
| 60 | + let quote; |
| 61 | + try { |
| 62 | + const quoteOfTheMonthDocSnap = await quoteOfTheMonthRef.get(); |
| 63 | + |
| 64 | + // Attach relevant debugging information with debug() |
| 65 | + debug("Monthly quote fetch result", { |
| 66 | + docRef: quoteOfTheMonthRef.path, |
| 67 | + exists: quoteOfTheMonthDocSnap.exists, |
| 68 | + createTime: quoteOfTheMonthDocSnap.createTime, |
| 69 | + }); |
| 70 | + |
| 71 | + if (quoteOfTheMonthDocSnap.exists) { |
| 72 | + quote = quoteOfTheMonthDocSnap.data().text; |
| 73 | + } else { |
| 74 | + // Use warn() for lower-severity issues than error() |
| 75 | + warn("Quote not found for month, sending default instead", { |
| 76 | + docRef: quoteOfTheMonthRef.path, |
| 77 | + dateRequested: today.toLocaleDateString("en-US"), |
| 78 | + }); |
| 79 | + |
| 80 | + quote = DEFAULT_QUOTE; |
| 81 | + } |
| 82 | + } catch (e) { |
| 83 | + // Attach stack trace to errors |
| 84 | + error("Unable to read quote from Firestore, sending default instead", |
| 85 | + {message: e.stack}); |
| 86 | + |
| 87 | + quote = DEFAULT_QUOTE; |
| 88 | + } |
| 89 | + |
| 90 | + // Attach relevant structured data to any log |
| 91 | + info("Sending a quote!", {quote: quote}); |
| 92 | + response.json({inspirationalQuote: quote}); |
| 93 | +}); |
| 94 | +// [END logsKitchenSink] |
| 95 | + |
| 96 | +// [START customLogWrite] |
| 97 | +exports.appHasARegression = onRegressionAlertPublished((event) => { |
| 98 | + write({ |
| 99 | + // write() lets you set additional severity levels |
| 100 | + // beyond the built-in logger functions |
| 101 | + severity: "EMERGENCY", |
| 102 | + message: "Regression in production app", |
| 103 | + issue: event.data.payload.issue, |
| 104 | + lastOccurred: event.data.payload.resolveTime, |
| 105 | + }); |
| 106 | +}); |
| 107 | +// [END customLogWrite] |
0 commit comments