13

I want to see Cloud Firestores logs like I can see for Cloud Functions for Firebase. There is a logs tab in Functions tab of the Firebase console. Similarly I want to see this for Cloud Firestore. How?

2 Answers 2

7

I wrote a firebase cloud function to log all activity to Firestore:

/**
 * Logs all database activity
 *
 * @type {CloudFunction<Change<DocumentSnapshot>>}
 */
exports.dbLogger = functions.firestore
  .document('{collection}/{id}')
  .onWrite(async (change, context) => {
    const {collection, id} = context.params;
    if (collection !== 'firestore_log') {
      const event = context.eventType;
      const data = change.after.data();
      const created_at = Date.now();
      admin.firestore().collection('firestore_log').add({collection, id, event, data, created_at});
    }
  });

Here's an example of the logged data:

firestore_log entry

Note that this is a quick dev only version that logs everything; in production we have Firestore rules to deny any read / write to the table (the admin on firebase functions can still access them):

// firestore.rules
match /firestore_log/{entryId} {
    allow read: if false;
    allow write: if false;
}

and filter the logged collections to avoid persisting sensitive data.

Note that if you prefer to persist this in the logs instead of Firestore you can use console.log({....}). I prefer Firestore because its designed to handle larger data sets.

0
1

There are currently no log entries for Cloud Firestore exposed to developers, so for the time being we recommend logging any statistics you are about separately.

3
  • 12
    In the case of a data breach/investigation, would these logs be available Dan?
    – Nth.gol
    Oct 10, 2018 at 13:53
  • 5
    @dan-mcgrath could we get an answer?
    – ditoslav
    Jan 21, 2019 at 18:26
  • Firestore logs may not be exposed, yet the ability to set Log Level is... firebase.google.com/docs/reference/js/… May 22, 2020 at 1:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.