Function Store
  • Home
  • Getting started
    • Introduction
    • Installing a function
    • Reconfiguring a function
    • Uninstalling a function
    • FAQs
  • Reference
    • Requirements
      • Assign Roles
        • User permissions
      • Link a Billing account to your project
      • Enable the APIs
    • Projects
      • How to add a project
      • How to set up a project
    • Functions
      • Benefits
      • Use cases
      • Authentication when installing
      • Runtime configuration
      • Deploying with a ZIP file
    • Policies
      • How to add a Policy
      • How to remove a Policy
    • Authentication
      • Users
      • Roles
      • Permissions
    • Secrets
      • Creating a Secret
      • Using a Secret
      • Changing the value for a Secret
      • Deactivating a Secret
  • developers
    • Overview
    • Getting Started
    • Set up
    • Workspaces
      • Create a workspace
      • Workspace boilerplate
      • Node engines
    • Writing Functions
      • Function Anatomy
      • Configuration file
      • Function Triggers
      • Environment Variables
        • Type "text"
        • Type "number"
        • Type "email"
        • Type "url"
        • Type "json"
        • Type "boolean"
        • Type "select"
        • Type "multiselect"
        • Type "firestoreDocumentPath"
        • Type "firestoreCollectionPath"
        • Type "color"
      • Documentation
    • Push/Update your functions in the cloud
    • Continuous Integration
    • Publish functions in the Marketplace
    • Emulators
      • Initialize emulators in your workspace
      • Environment variables
      • Starting emulators
      • Testing your functions in the emulator
      • Importing existing data into local emulators
        • Exporting data from Cloud Firestore to local emulator
        • Exporting user accounts from Firebase to local emulator
    • Visual Studio Code Extension
      • Installing the extension
      • Extension UI
      • Functions list
      • Creating and editing functions
      • Emulators
      • Extension settings
      • Troubleshooting
        • "Command not found" error when executing a command
  • Billing
    • Understanding Billing
  • Security
    • Security
  • Resources
    • Events and Triggers
    • Best Practices
      • How to log events in functions?
    • Document your Functions
    • Editorial guidelines
    • Our Processes
      • Use Cases Analysis
  • Use cases
    • Custom Stripe Checkout
Powered by GitBook
On this page
  • How should you log events?
  • How should I log errors?

Was this helpful?

  1. Resources
  2. Best Practices

How to log events in functions?

Logging events in a system is an important task to get the most useful insights into what is happening in the application. We recommend following some conventions to achieve a consistent way of logging events.

Using the Firebase functions logger, pass to the first argument a string with the event description using capital letters and underscore between the words, and to the second argument, pass any data you want to log:

Logger.info('EVENT_DESCRIPTION', {
    message,
    userId,
});

How should you log events?

For example, you may want to log an event when a Stripe charge is made. You can then pass the customer ID and the amount of the charge:

const Logger = require('firebase-functions/lib/logger');

const confirmPayment = async ({ customerId, amount }) => {
    // payment logic
    await stripe.charge({
        customer: customerId,
        amount,
    });
    Logger.info('CUSTOMER_CHARGED', {
        customerId,
        amount,
    });
}

module.exports = confirmPayment;

How should I log errors?

Errors are logged the same as the above events, but the description starts with the ERROR_ prefix, and the data should have a message with an error string:

const Logger = require('firebase-functions/lib/logger');
const admin = require('./admin');

const getUserByEmail = (email) => {
    try {
        const user = await admin.auth().getUserByEmail(email);
        return user.toJSON();
    } catch (error) {
        Logger.error('ERROR_GET_USER_BY_EMAIL', {
            errorMessage: error.toString(),
        });
        return null;
    }
}

module.exports = getUserByEmail;
PreviousBest PracticesNextDocument your Functions

Last updated 3 years ago

Was this helpful?