1

This one came up in a code review and I'm dragging the internet into the argument.

Given that...

Node.js documentation for console.log says that console.log will pass its non-string arguments to util.format.

Node.js documentation for util.format mentions that util.format is intended as a debugging tool.

Node.js documentation for util.inspect says that console.log and util.format call into util.inspect and furthermore claims:

util.inspect(object[, options])

The util.inspect() method returns a string representation of object that is intended for debugging. The output of util.inspect may change at any time and should not be depended upon programmatically.

(emphasis is mine, and I'm already familiar the answers to Difference between "process.stdout.write" and "console.log" in node.js?)

Does that mean...

Does that mean that it is inappropriate to write the simplest thing like console.log(1) if the output is to be consumed by another process programmatically? ...because the arguments are being passed to util.inspect, which seems to reserve the right to change the format of the output at any time? Any non-string argument, would seem to be a no-no. And "may change at any time" seems to reserve enough freedom that it might start outputting random emojis tomorrow if it feels like it.

Do we get any guarantees at all of what the output from console.log will be when passing a non-string argument?

Wyck
  • 10,311
  • 6
  • 39
  • 60
  • 1
    "Does that mean that it is inappropriate to write the simplest thing like console.log(1) if the output is to be consumed by another process programmatically?" **I would tend to say yes, it's inappropriate.** You should use a Logger if you intend to consume logged data programmatically. At the very least, this logger can wrap `console.log` for the time being, but it then gives you the freedom to change its implementation (thus adapt to changes in `util.inspect`) if need be, without having to change all uses of the Logger in your code. That being said, `util.inspect` is unlikely to change... – zr0gravity7 Jul 26 '21 at 15:03
  • Technically - no. There is no standard for consoles. `console.log(1)` *might* log `1.00` if the environment implementation thinks this is the most useful way to show this. Now, this is *very unlikely* and you might be reasonably safe if you just want to consume simple strings but at the same time...why not just be safe**r** and not rely on debug tools? – VLAZ Jul 26 '21 at 15:05
  • 1
    Full disclosure of bias: I currently stand on the side of the argument that **it is inappropriate** to depend on the output of console.log if it's being consumed programmatically. I'm interested if there's any evidence or standard to the contrary, or if there's more evidence to support my position. – Wyck Jul 26 '21 at 15:10
  • @Wyck it's very hard to prove a negative here. Or might be quite easy if people are being reasonable. *There is no standard for `console.log` output*. So, we cannot point you to some specific resource that says there is no one way consoles work. But *the lack* of such a resource is a pretty good evidence by itself. – VLAZ Jul 26 '21 at 15:13
  • @VLAZ The fact that there's [no standard](https://stackoverflow.com/questions/30269972/why-is-console-non-standard) does not mean there's no guarantee. We're talking specifically about node.js here and what is promised about its behaviour. – Wyck Jul 26 '21 at 15:39

0 Answers0