节点。js控制台。日志vs console.info

时间:2021-11-21 20:47:24

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

使用控制台的好处是什么?日志vs console.info吗?或者其他的控制台命令都是这样的吗?

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

vs

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

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

they seem to all do the same as console.log

他们似乎都和console.log一样

6 个解决方案

#1


37  

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

根据你链接的文档,控制台。错误和控制台。警告stderr输出。其余的输出到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.

在浏览器和Node.js中都有很多JavaScript要运行。让node实现完整控制台可以实现更大的代码交叉兼容性。

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


31  

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

log()比console.info()短

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

它们是一样的,这是唯一的优势

节点。js控制台。日志vs console.info

#3


13  

According to the docs it's pretty clear.

根据医生的说法,这很明显。

console.info([data], [...])# Same as console.log.

console.info([data],[…])#和console.log一样。

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

控制台。错误([数据],[…])#与控制台相同。日志但是打印到stderr。

console.warn([data], [...])# Same as console.error.

控制台。警告([数据],[…])#和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.

这意味着没有好处也没有坏处。info == log,和warn == error。除非您想要打印到stderr,否则info和或log都可以工作。

#4


10  

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.

不过,还有一些轻量级模块,可以为console.info、console添加蓝色、橙色和红色。警告,还有控制台。错误分别。通过这种方式,控制台API就像客户端一样。

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

节点。js控制台。日志vs console.info

#5


7  

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.

除了被接受的答案之外,还有一个细节:在Chrome和FireFox中,console.info的日志行是用一个小的i图标作为控制台的前缀。日志行不。警告线和错误线分别用一个小三角形和x作为前缀。

#6


6  

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.

在控制台。log和console.info可能没有什么不同,除了颜色之外还有其他用途。例如,当使用像eslint这样的linter时,您可以设置控制台。记录以提供警告消息。假设您只想使用控制台。为您的开发目的登录,并使用console.info来获取最终用户可能需要的信息。使用linter,您现在有了一个可见的、直接的临时控制台提醒。在开发期间帮助您的日志,但必须在提交/发布之前删除。

#1


37  

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

根据你链接的文档,控制台。错误和控制台。警告stderr输出。其余的输出到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.

在浏览器和Node.js中都有很多JavaScript要运行。让node实现完整控制台可以实现更大的代码交叉兼容性。

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


31  

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

log()比console.info()短

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

它们是一样的,这是唯一的优势

节点。js控制台。日志vs console.info

#3


13  

According to the docs it's pretty clear.

根据医生的说法,这很明显。

console.info([data], [...])# Same as console.log.

console.info([data],[…])#和console.log一样。

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

控制台。错误([数据],[…])#与控制台相同。日志但是打印到stderr。

console.warn([data], [...])# Same as console.error.

控制台。警告([数据],[…])#和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.

这意味着没有好处也没有坏处。info == log,和warn == error。除非您想要打印到stderr,否则info和或log都可以工作。

#4


10  

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.

不过,还有一些轻量级模块,可以为console.info、console添加蓝色、橙色和红色。警告,还有控制台。错误分别。通过这种方式,控制台API就像客户端一样。

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

节点。js控制台。日志vs console.info

#5


7  

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.

除了被接受的答案之外,还有一个细节:在Chrome和FireFox中,console.info的日志行是用一个小的i图标作为控制台的前缀。日志行不。警告线和错误线分别用一个小三角形和x作为前缀。

#6


6  

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.

在控制台。log和console.info可能没有什么不同,除了颜色之外还有其他用途。例如,当使用像eslint这样的linter时,您可以设置控制台。记录以提供警告消息。假设您只想使用控制台。为您的开发目的登录,并使用console.info来获取最终用户可能需要的信息。使用linter,您现在有了一个可见的、直接的临时控制台提醒。在开发期间帮助您的日志,但必须在提交/发布之前删除。