Nodejs- console.error vs util.debug

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?


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)

Sources

Console

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;

Utils

https://github.com/joyent/node/blob/master/lib/util.js

exports.debug = function(x) {
  process.stderr.write('DEBUG: ' + x + '\n');
};

Log Functions:

Console

exports.log = function() {
  process.stdout.write(format.apply(this, arguments) + '\n');
};

Utils

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');
  }
};

Why

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.

So how do you know which one you need?

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


Joe Lapp's comment to the currently accepted answer deserves to be raised as an answer:

FYI: utils.print, .puts, .debug, and .error are now all deprecated. console.log is advised instead of .print and .puts, and console.error is advised instead of .debug and .error. See the utils code

References:

The Node.js Docs

The current utils code


The console methods are more robust in that you can pass n arguments and they will be concatenated before being written to stdout or stderr, but they are both convenience wrappers around the same underlying calls to stdout and stderr.

.log, .info, and .dir are non blocking.
.warn and .error are blocking.

console.log and util.log both write using process.stdout.write. console.error and util.debug both write using process.binding("stdio").writeError.