I noticed that node.js has both console.error
and util.debug
, as well as console.log
and util.log
.
Is there a difference besides the console.* functions being more robust in the parameters they take? The API says that they write to stdout and stderr respectively.
If there's no difference, which should I use and why?
Technically console. debug() console.info() and console. log() are identical - the only difference is that debug messages are hidden by default and log messages are visible in the recent versions of Chrome (to see debug messages, you have to set the log level to Verbose in the Devtools).
Node. js console is a global object and is used to print different levels of messages to stdout and stderr. There are built-in methods to be used for printing informational, warning, and error messages.
log() function from console class of Node. js is used to display the messages on the console. It prints to stdout with newline. Parameter: This function contains multiple parameters which are to be printed.
Open up Preferences > Settings and in the search box type in “node debug”. Under the Extensions tab there should be one extension titled “Node debug”. From here, click the first box: Debug > Node: Auto Attach and set the drop down to “on”. You're almost ready to go now.
They're two different functions, that do two different things. Learn to read the source. It will help you a lot (even in languages like C# with reflector)
https://github.com/joyent/node/blob/master/lib/console.js
Console.prototype.warn = function() { this._stderr.write(util.format.apply(this, arguments) + '\n'); }; Console.prototype.error = Console.prototype.warn;
https://github.com/joyent/node/blob/master/lib/util.js
exports.debug = function(x) { process.stderr.write('DEBUG: ' + x + '\n'); };
exports.log = function() { process.stdout.write(format.apply(this, arguments) + '\n'); };
exports.log = function(msg) { exports.puts(timestamp() + ' - ' + msg.toString()); }; exports.puts = function() { for (var i = 0, len = arguments.length; i < len; ++i) { process.stdout.write(arguments[i] + '\n'); } };
As with every other Unix oriented system, which node is most definitely geared in the guise of - see the many comments from Ryan on the topic, the logging functions are given in the same guise. There are two basic classes for logging, and they both do effectively the same thing, but for different reasons. To the casual observer, they are the same, but they're not really.
Console logging is intended to be used during debugging. This will go to STDOUT and will show your statements on the REPL1 console, useful for debugging.
Utility logging is intended to be used during standard services runtime. They will goto the process STDOUT, which often is a log file for the process.
But since this can be outwardly overridden at need, and because it will be different in the future (most likely) for Windows processes (given the new ports and developments) and other systems, then you should try to use these methods as the de facto way to write out to the logs during normal runtime. Examples of how it will be different in Windows include the use of the system log for logging, as opposed to a straight logfile.
If you're planning on running this in the REPL for debugging, use the console logger. If you're intending to start the service and forget about it, then use the utils logging.
1 – Read Evaluate Print Loop
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