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?
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With