Node.js console.log vs console.info

Javascriptnode.jsTerminalConsole

Javascript Problem Overview


What is the benefit of using console.log vs console.info? Or any of the other console commands for that matter?

console.info("info");
console.error("error");
console.warn("warn");

vs

console.log("log");

I thought it might change the color of the output or concatenate some sort of label, but they seem to all do the same thing. And according to the documentation here:

https://nodejs.org/api/console.html#console_console_info_data

they seem to all do the same as console.log

Javascript Solutions


Solution 1 - Javascript

According to the documentation that you linked to, console.error and console.warn outputs to stderr. The others output to stdout.

If you are doing piping or redirection from node.js the difference is important.

There is a lot of JavaScript written to run in both the browser and Node.js. Having node implement the full console allows for greater code cross-compatibility.

In most browsers, not only do these log in different colors, but you can also filter to see specific messages.

console.info("info");
console.error("error");
console.warn("warn");
console.log("log");

Solution 2 - Javascript

While console.log and console.info might not be different, there are other uses except mere coloring. For example, when using a linter like eslint, you can set console.log to provide a warning message. Let's say you only want to use console.log for your development purposes and use console.info for information that end users might need. With a linter you now have a visible and direct reminder of your temporary console.log that aid you during development, but must be removed before commits/publishing.

Solution 3 - Javascript

console.log() is shorter than console.info()

They're the same thing, and that's the only advantage.

enter image description here

Solution 4 - Javascript

According to the docs it's pretty clear.

> console.info([data], [...])# > Same as console.log. > > console.error([data], [...])# > Same as console.log but prints to stderr. > > console.warn([data], [...])# > Same as console.error.

This means there is no benefit or downside. info == log, and warn == error. Unless you want to print to stderr, info and or log will work.

Solution 5 - Javascript

Visually, No difference actually among console.log , console.info , console.warn , as well as console.error regarding the server side(terminal).

However, there are lightweight modules that add blue, orange and red colors for console.info , console.warn , as well as console.error respectively . By that, console API behaves like client-side.

 npm i console-info console-warn console-error --save-dev;

enter image description here

Solution 6 - Javascript

One more detail in addition to the accepted answer: In Chrome and FireFox, console.info log lines are prefixed with a little i icon while console.log lines are not. warn and error lines are prefixed with a little triangle and x, respectively.

Solution 7 - Javascript

stdin A readable stream for reading input from the user.

stdout A writable stream, either synchronously or asynchronously.

stderr A blocking synchronous writable stream intended for error messages.

The stdout or non-blocking functions are: console.log, console.info, util.puts, util.print and Stderr.

The blocking functons are: console.warn, console.error, util.debug and process.stdin (a readable stream for getting user input).

Solution 8 - Javascript

It has been established that log and info are basically the same thing, but I'm not sure that completely answers the question:

> What is the benefit of using console.log vs console.info?

One benefit, apart from what has already been mentioned, is you can use each for a different purpose. For example, you might use console.log just for quick debugging and spitting things out to the console, while you might use console.info for permanent messages you want to output to the console in your code, such as information on the current app status. Then when you have a situation where a random object is printed in your console and you realize you've left a log statement in there somewhere by accident, you can do a global search for 'console.log' and delete every instance and have confidence you didn't delete anything important that you meant to leave in there.

Solution 9 - Javascript

There is a difference for React devs. It comes from an issue in the react devtool extension and at least affects Create-React-App users, not sure if it's all web pack.

Issue is mentioned here: https://stackoverflow.com/questions/69071151/react-devtools-console-log-from-react-devtools-backend-js4049

but the jist is: console.log will always report its source as

react_devtools_backend.js:4049 

wheres console.info will have the actual filename and line number you're logging from.

Solution 10 - Javascript

I have seen where console.log is for temporarily logging of state information for debugging.

console.info is a more permanent thing - such as saying what port something is running on, and something you wouldn't cut out after you are done debugging.

This makes it easy to clean up your code for committing it. You can even have your linter have a rule to prevent console.log from being committed.

Solution 11 - Javascript

The different logging levels let you manage the noise level in your console: In both the Firefox (I'm using 78 right now) and Chrome (84) devtools, the js console lets you choose what "debug level" of output you want to see. FF lets you toggle the visibility of console.error, .warn, .log, .info, and .debug messages by clicking individual buttons for each (that show you how many were suppressed, when "off"), whereas Chrome has a dropdown with checkmarks next to the items (.info and .log are controlled by the "Info" one, and .debug by "Verbose"). The Chrome dropdown label ("All levels" or whatever you set) turns red if output was suppressed.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionseantomburkeView Question on Stackoverflow
Solution 1 - JavascriptJeremy J StarcherView Answer on Stackoverflow
Solution 2 - JavascriptFigidonView Answer on Stackoverflow
Solution 3 - JavascriptMaxView Answer on Stackoverflow
Solution 4 - JavascriptSterling ArcherView Answer on Stackoverflow
Solution 5 - JavascriptAbdennour TOUMIView Answer on Stackoverflow
Solution 6 - JavascriptBill HoagView Answer on Stackoverflow
Solution 7 - JavascriptAvinash MauryaView Answer on Stackoverflow
Solution 8 - JavascriptdallinView Answer on Stackoverflow
Solution 9 - JavascriptsirclesamView Answer on Stackoverflow
Solution 10 - JavascriptRobKohrView Answer on Stackoverflow
Solution 11 - JavascriptTom HundtView Answer on Stackoverflow