如何查看Cloud Firestore日志?

Imm*_*ohn 5 firebase google-cloud-firestore

我想要看到Cloud Firestores日志,就像可以看到Firebase的Cloud Functions一样。Firebase控制台的“功能”选项卡中有一个“日志”选项卡。同样,我想在Cloud Firestore中看到这一点。怎么样?

For*_*man 5

我编写了一个 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});
    }
  });
Run Code Online (Sandbox Code Playgroud)

以下是记录数据的示例:

firestore_log 条目

请注意,这是一个仅用于记录所有内容的快速开发版本;在生产中,我们有 Firestore 规则来拒绝对表的任何读/写(firebase 功能的管理员仍然可以访问它们):

// firestore.rules
match /firestore_log/{entryId} {
    allow read: if false;
    allow write: if false;
}
Run Code Online (Sandbox Code Playgroud)

并过滤记录的集合以避免持久保存敏感数据。

请注意,如果您更喜欢将其保留在日志中而不是 Firestore 中,则可以使用console.log({....}). 我更喜欢 Firestore,因为它旨在处理更大的数据集。


Dan*_*ath 2

目前没有向开发人员公开的 Cloud Firestore 日志条目,因此目前我们建议单独记录您所涉及的任何统计信息。

  • 在发生数据泄露/调查的情况下,这些日志是否可用,丹? (15认同)
  • @dan-mcgrath 我们能得到答案吗? (5认同)