|
| 1 | +from firebase_functions import https_fn |
| 2 | +from firebase_functions.alerts import crashlytics_fn |
| 3 | +from firebase_admin import initialize_app, firestore |
| 4 | +import datetime |
| 5 | +import sys |
| 6 | + |
| 7 | +# [START loggerImport] |
| 8 | +from firebase_functions import logger |
| 9 | +# [END loggerImport] |
| 10 | + |
| 11 | +app = initialize_app() |
| 12 | + |
| 13 | + |
| 14 | +# [START helloLogs] |
| 15 | +@https_fn.on_request() |
| 16 | +def hello_world(req: https_fn.Request) -> https_fn.Response: |
| 17 | + # sends a log to Cloud Logging |
| 18 | + logger.log("Hello logs!") |
| 19 | + |
| 20 | + return https_fn.Response("Hello from Firebase!") |
| 21 | +# [END helloLogs] |
| 22 | + |
| 23 | + |
| 24 | +# [START logsKitchenSink] |
| 25 | +@https_fn.on_request() |
| 26 | +def get_inspirational_quote(req: https_fn.Request) -> https_fn.Response: |
| 27 | + firestore_client = firestore.client() |
| 28 | + today = datetime.date.today() |
| 29 | + quote_of_the_month_ref = (firestore_client.collection("quotes").doc(str( |
| 30 | + today.year)).collection("months").doc(str(today.month))) |
| 31 | + |
| 32 | + default_quote = "Python has been an important part of Google since the beginning, and remains so as the system grows and evolves." |
| 33 | + |
| 34 | + quote = None |
| 35 | + try: |
| 36 | + quote_of_the_month = quote_of_the_month_ref.get() |
| 37 | + |
| 38 | + # Attach relevant debugging information with debug() |
| 39 | + logger.debug( |
| 40 | + "Monthly quote fetch result", |
| 41 | + docRef=quote_of_the_month.path, |
| 42 | + exists=quote_of_the_month.exists, |
| 43 | + createTime=quote_of_the_month.createTime, |
| 44 | + ) |
| 45 | + |
| 46 | + if quote_of_the_month.exists: |
| 47 | + quote = quote_of_the_month.to_dict()["text"] |
| 48 | + else: |
| 49 | + # Use warn() for lower-severity issues than error() |
| 50 | + logger.warn( |
| 51 | + "Quote not found for month, sending default instead", |
| 52 | + doc_reference=quote_of_the_month.path, |
| 53 | + date_requested=today.strftime("%Y-%m-%d"), |
| 54 | + ) |
| 55 | + quote = default_quote |
| 56 | + except: |
| 57 | + e = sys.exc_info()[0] |
| 58 | + # [START logError] |
| 59 | + # Attach an error object as the second argument |
| 60 | + logger.error("Unable to read quote from Firestore, sending default instead", error=e) |
| 61 | + # [END logError] |
| 62 | + quote = default_quote |
| 63 | + |
| 64 | + # Attach relevant structured data to any log |
| 65 | + logger.info("Sending a quote!", quote=quote) |
| 66 | + return https_fn.Response("Hello from Firebase!") |
| 67 | +# [END logsKitchenSink] |
| 68 | + |
| 69 | + |
| 70 | +# [START customLogWrite] |
| 71 | +@crashlytics_fn.on_regression_alert_published() |
| 72 | +def app_has_regression(alert: crashlytics_fn.CrashlyticsRegressionAlertEvent) -> None: |
| 73 | + logger.write( |
| 74 | + severity="EMERGENCY", |
| 75 | + message="Regression in production app", |
| 76 | + issue=alert.data.payload.issue, |
| 77 | + last_occurred=alert.data.payload.resolve_time, |
| 78 | + ) |
| 79 | + print(alert) |
| 80 | +# [END customLogWrite] |
0 commit comments