Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I see Cloud Firestore logs?

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?

like image 587
Immanuel John Avatar asked Jul 30 '18 07:07

Immanuel John


1 Answers

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.

like image 109
ForrestLyman Avatar answered Oct 18 '22 17:10

ForrestLyman