Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs- console.error vs util.debug

Tags:

node.js

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?

like image 980
beatgammit Avatar asked Jul 15 '11 03:07

beatgammit


People also ask

What is the difference between console info and console log?

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).

What is console in node JS?

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.

How does Nodejs console log work?

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.

How do I debug Node JS in terminal?

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.


1 Answers

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

like image 57
jcolebrand Avatar answered Oct 12 '22 03:10

jcolebrand