node.js 控制台_如何在node.js中使用控制台

本文详细介绍了Node.js中的console模块,包括console.log、console.info、console.debug等方法,以及console.table、console.dir、console.error、console.warn等功能的使用,通过实例展示了如何有效地利用控制台进行日志输出、数据展示和调试。
摘要由CSDN通过智能技术生成

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版。 0353877和nodeJS版本811 。** 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'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值