Does anyone know how to log JSON data in a readable manner to the firebase logs? When using:
console.log(req.body)
or
console.log(`${req.body.event}: ${JSON.stringify(req.body, null, 2)}`);
it prints it as seen in the picture below on multiple lines.
My firebase cloud functions run on Node 10
I can confirm I have the same problem, since the update to Node 10 JSON objects are printed in multiple lines.
For now the solution is to import this dependency:
require('firebase-functions/lib/logger/compat');
Just put this at the top of your functions code.
You can check the official issue for updates: https://github.com/firebase/firebase-functions/issues/612
You are supposed to use functions.logger.log()
with Node version 10 and above. See: https://firebase.google.com/docs/functions/writing-and-viewing-logs
This multi-line behavior is likely due to the fact that you explicitly tell JSON.stringify() to add line breaks and indentation. The last parameter (2) tells JSON formatter that you want "structured" (aka "pretty") output with 2 spaces per indentation level. If you drop the last argument (you may safely drop "null" as well), then you should see good old one long string :)
FWIW, I don't see this logging behavior when using NodeJS 8 on my Firebase Functions. So something changed with Node 10 on the Firebase side... Also check this GitHub issue: https://github.com/firebase/firebase-functions/issues/612
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