Node.js console.log vs console.info

眉间皱痕 提交于 2019-12-31 08:40:33

问题


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


回答1:


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");



回答2:


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

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




回答3:


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.




回答4:


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;




回答5:


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.logs that aid you during development, but must be removed before commits/publishing.




回答6:


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.




回答7:


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



来源:https://stackoverflow.com/questions/25532778/node-js-console-log-vs-console-info

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!