node.js 控制台
介绍 ( Introduction )
In this article, we'll learn how to use most methods available in the nodeJS console class more effectively.
在本文中,我们将学习如何更有效地使用nodeJS控制台类中可用的大多数方法。
To demonstrate, I'll use Chrome browser version 70.0.3538.77 and nodeJS version 8.11.** 3**.
为了演示,我将使用Chrome浏览器70版。 0 。 3538 。 77和nodeJS版本8 。 11 。** 3 **。
Okay then. Let's begin.
好吧。 让我们开始。
console.log,console.info和console.debug ( console.log, console.info and console.debug )
console.log(string, substitution)
While the famed console.log
method really needs no introduction, you'll be glad to know that the console.info and console.debug methods are identical to console.log in their operation.
虽然确实不需要介绍著名的console.log
方法,但是您会很高兴知道console 。 信息和控制台 。 调试方法与console相同。 登录他们的操作。
You can use console.debug in the Firefox browser console by default but to use it in Chrome, you'll have to set the log level to verbose like this.
您可以使用console 。 默认情况下,在Firefox浏览器控制台中进行调试 ,但要在Chrome中使用它,则必须将日志级别设置为冗长,如下所示。
The console.log method prints to standard out, whether this be the terminal or browser console. It outputs strings by default but can be used in conjuction with template strings to modify what it returns.
控制台 。 log方法输出到标准输出,无论是终端还是浏览器控制台。 默认情况下,它输出字符串,但可以与模板字符串结合使用以修改其返回的内容。
Here's how it works:
运作方式如下:
The arguments in the template string are passed to util.format which then processes the arguments by replacing each substitution token with the respective converted value. The supported substitution tokens are:
模板字符串中的参数传递给util.format ,然后通过将每个替换标记替换为各自的转换值来处理参数。 支持的替换令牌是:
%s (%s)
const msg = `Using the console class`;
console.log('%s', msg); // Using the console class
console.log(msg); // Using the console class
%s is the default substitution pattern.
%s是默认的替换模式。
%d,%f,%i,%o (%d, %f, %i, %o)
const circle = (radius = 1) => {
const profile = {
};
const pi = 22/7;
profile.diameter = 2_pi_radius;
profile.circumference = pi_radius_2;
profile.area = pi_radius^2;
profile.volume = 4/3_pi_radius^3;
console.log('This circle has a radius of: %d cm', radius);
console.log('This circle has a circumference of: %f cm', profile.diameter);
console.log('This circle has an area of: %i cm^2', profile.area);
console.log('The profile of this cirlce is: %o', profile);
console.log('Diameter %d, Area: %f, Circumference %i', profile.diameter, profile.area, profile.circumference)
}
circle();
This is what happens:
这是发生了什么:
%d will be substituted by a digit (integer or float). %f will be replaced by a float value. %i will be replaced by an integer. %o will be replaced by an Object.
%d将替换为数字(整数或浮点数)。 %f将被浮点值替换。 %i将被替换为整数。 %o将被Object取代。
%o is especially handy because we don't have to use JSON.stringify to expand our object because it shows all the object's properties by default.
%o特别方便,因为我们不必使用JSON.stringify来扩展对象,因为它默认情况下会显示所有对象的属性。
Note that you can use as many token substitutions as you like. They'll just be replaced the same order as the arguments you pass.
请注意,您可以根据需要使用任意数量的令牌替换。 它们将被替换为与您传递的参数相同的顺序。
%C (%c)
This substitution token applies css styles to the subsituted text.
此替换标记将CSS样式应用于替换的文本。
console.log('LOG LEVEL: %c OK', 'color: green; font-weight: normal');
console.log('LOG LEVEL: %c PRIORITY', 'color: blue; font-weight: medium');
console.log('LOG LEVEL: %c WARN', 'color: red; font-weight: bold');
console.log('ERROR HERE'