【发布时间】:2018-07-30 07:25:21
【问题描述】:
我想查看 Cloud Firestores 日志,就像我在 Cloud Functions for Firebase 中看到的那样。 Firebase 控制台的 Functions 选项卡中有一个日志选项卡。同样,我想为 Cloud Firestore 看到这个。怎么样?
【问题讨论】:
标签: firebase google-cloud-firestore
我想查看 Cloud Firestores 日志,就像我在 Cloud Functions for Firebase 中看到的那样。 Firebase 控制台的 Functions 选项卡中有一个日志选项卡。同样,我想为 Cloud Firestore 看到这个。怎么样?
【问题讨论】:
标签: firebase google-cloud-firestore
我编写了一个 Firebase 云函数来将所有活动记录到 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});
}
});
以下是记录数据的示例:
请注意,这是一个只记录所有内容的快速开发版本;在生产中,我们有 Firestore 规则来拒绝对表的任何读/写(firebase 功能的管理员仍然可以访问它们):
// firestore.rules
match /firestore_log/{entryId} {
allow read: if false;
allow write: if false;
}
并过滤记录的集合以避免保留敏感数据。
请注意,如果您希望将其保留在日志中而不是 Firestore 中,则可以使用 console.log({....})。我更喜欢 Firestore,因为它旨在处理更大的数据集。
【讨论】:
目前没有向开发人员公开 Cloud Firestore 的日志条目,因此我们暂时建议您单独记录您所了解的所有统计信息。
【讨论】: