6

We have our firebase function console.log() an object near the end of the execution.

In the past it always clumped the entire object into one single dropdown in the Firebase Functions Log interface but now it seems to put each key of the object in a separate line making it not only take up more space but quite unreadable.

This seems to happen mostly when objects are stringified like:

console.log(JSON.stringify({key1: 'val1', key2: 'val2'}))

Below is an example of an error object being logged:

enter image description here

How can this madness be put into a single dropdown again?

We are using:

"firebase-admin": "^8.6.1",
"firebase-functions": "^3.3.0",

Thank you!

T Mack
  • 950
  • 3
  • 12
  • 27

3 Answers3

3

If you have an object to log, I recommend just pass it directly to console.log(). JSON.stringify() might be adding carriage returns, which could be interpreted as multiple lines to log.

console.log({key1: 'val1', key2: 'val2'})

Just make sure that the object doesn't contain references to other very complex objects (especially self-referential objects), otherwise it could cause problems with evaluating the final string at runtime.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
1

Neither console.log(data) nor console.log(JSON.stringify(data)) worked for me. Using the sdk logger worked for me:

import * as functions from 'firebase-functions';
functions.logger.log(data);

As described here: https://firebase.google.com/docs/functions/writing-and-viewing-logs#logger-sdk

user1689987
  • 1,002
  • 1
  • 8
  • 23
0

If you put

require('firebase-functions/lib/logger/compat');

at the top of your functions code with the latest version of the sdk logs should come in 1 single line

src: https://github.com/firebase/firebase-functions/issues/612#issuecomment-646652675

Ishaan Garg
  • 3,014
  • 3
  • 25
  • 28