PatS
PatS

Reputation: 11434

NodeJS: Where does console.debug output go?

Where does console.debug output go? And how can it be enabled to appear on the stdout, like console.log?

I looked it up in https://nodejs.org/api/console.html, but it does not appear. If, however, I run node from the command line, it is defined.

node
console.debug
[Function: debug]
console.debug('but where does this go?')

So the function is defined, but (by default) doesn't output to stdout or stderr. I'm guessing there is a flag to enable to turn it on.

Upvotes: 11

Views: 11228

Answers (1)

josh3736
josh3736

Reputation: 144832

Starting with node v8.0.0 and prior to v8.10 and v10.x (depending on the specific method), the global console exposed many of the same methods that are exposed in browsers, but did not document their existence or behavior. Such methods include:

console.debug                 console.dirxml                console.markTimeline
console.profile               console.profileEnd            console.table
console.timeStamp             console.timeline              console.timelineEnd 

The implementation for these methods were not in node (they did not appear in lib/console.js); they were added by V8 itself and were copied onto node's global console object.

As you've observed, calling these methods did not result in anything going to stdout. Instead, output goes to any attached inspectors.

This means you have to run node with the --inspect flag, open Chrome and go to chrome://inspect, then attach the inspector to your process. You will see the output of the extended console methods in the inspector's console.

Keep in mind that by default, the inspector console filters out calls to console.debug. You need to click the levels dropdown and check Verbose in order to see console.debug output.

Alternatively, you could:

  • In node v8.0-8.9, do something like console.debug = console.log if you just want to see output on stdout
  • update to node v8.10

As of v8.10.0, the console.debug method is implemented by node and its output appears on stdout. The GUI inspector retains its behavior (messages are hidden unless you change the logging level).

node v10.x implements many of the other methods listed above, which means these methods are now work as expected (ie show up on stdout) and are documented.

Upvotes: 13

Related Questions