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?
Asked
Active
Viewed 6,926 times
2 Answers
5
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.
ForrestLyman
- 1,424
- 16
- 22
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.
Dan McGrath
- 39,648
- 10
- 95
- 126
-
11In the case of a data breach/investigation, would these logs be available Dan? – Nth.gol Oct 10 '18 at 13:53
-
5@dan-mcgrath could we get an answer? – ditoslav Jan 21 '19 at 18:26
-
Firestore logs may not be exposed, yet the ability to set Log Level is... https://firebase.google.com/docs/reference/js/firebase.firestore#setloglevel – Greg Fenton May 22 '20 at 01:24