Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging a JSON object in firebase console

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

enter image description here

like image 818
FriedrichCoen Avatar asked Feb 10 '20 12:02

FriedrichCoen


4 Answers

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

like image 154
lorenzo biondani Avatar answered Oct 16 '22 16:10

lorenzo biondani


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

like image 38
seveneights Avatar answered Oct 16 '22 17:10

seveneights


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 :)

like image 1
Alex Stepanov Avatar answered Oct 16 '22 16:10

Alex Stepanov


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

like image 1
Kevin Boosten Avatar answered Oct 16 '22 16:10

Kevin Boosten