Home > Software engineering >  Custom logger in Firestore to create statistics on operations?
Custom logger in Firestore to create statistics on operations?

Time:09-29

How can I sniff all traffic in Firestore JS SDK?

To explain in more detail, the following code enables verbose logging into console.

import { setLogLevel } from 'firebase/firestore'
setLogLevel('debug')

Now, everytime a network communication happens, in the console there are outputs like:

[2022-09-28T12:00:21.878Z] @firebase/firestore: Firestore (9.6.10): Connection WebChannel received: {"documentChange":{"document":{"name":"projects/mu-project/databases/(default)/documents/my_collection/sHM7OvtpEygAKQBawxha","fields":{ ...

Is it possible to subscribe to such logs with a custom callback, instead of printing it in console, so I can analyze the communication?

Something like:

import { setLogLevel, setLogger } from 'firebase/firestore'
setLogLevel('debug')
setLogger((message, payload) => {
 // custom stuff
})

It is intended to create statistics on writes and reads, that are intended to be used on FE in realtime.

CodePudding user response:

It's possible in that you can build your own version of the SDK and make it do whatever you want. It's open source.

Otherwise, no, it's not possible.

You're probably better off instrumenting your own application code to make it capture the things it's doing with Firestore.

CodePudding user response:

I have found onLog function which can be used to register a callback on every produced log message. However, it does not solve the question precisely, because the function does not intercept the logging, and therefore, cannot mute the output to the console. It will produce a lot of debug messages which is not desired in the production deployment.

import { onLog, setLogLevel } from 'firebase/app'

setLogLevel('debug') // must be used to track the traffic
onLog((args) => {
  console.log('Custom log callback: ', args)
}, {level: 'debug'})

Also note that onLog must be called after the Firestore component is initialized.

  • Related