亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home WeChat Applet Mini Program Development How to print log information in console

How to print log information in console

Jun 04, 2018 pm 03:16 PM
console javascript node.js

這次給大家?guī)韈onsole怎樣打印日志信息,console打印日志信息的注意事項(xiàng)有哪些,下面就是實(shí)戰(zhàn)案例,一起來看一下。

我們首先創(chuàng)建如下文件:

//?index.js
let?fs?=?require('fs');
let?options?=?{
??flags:?'a',?????//?append模式
??encoding:?'utf8',??//?utf8編碼
};
let?stdout?=?fs.createWriteStream('./stdout.log',?options);
let?stderr?=?fs.createWriteStream('./stderr.log',?options);
//?創(chuàng)建logger
let?logger?=?new?console.Console(stdout,?stderr);
for?(let?i?=?0;?i?<?100;?i++)?{
??logger.log(`log?message?${i}`);
??logger.error(`err?message?${i}`);
}

在上面代碼中,我們其實(shí)是創(chuàng)建了一個(gè)console.Console類的實(shí)例,該類需要指定兩個(gè)參數(shù),即標(biāo)準(zhǔn)輸出流和標(biāo)準(zhǔn)錯(cuò)誤輸出流,正常情況下,實(shí)際上是對(duì)應(yīng)了process.stdout和process.stderr,以上的代碼中,我們將這兩個(gè)輸出流改為了文件輸出流,并指定為文件追加模式,這樣即可將日志信息輸出到指定的文件中去。運(yùn)行上面的代碼,會(huì)生成stdout.log和stderr.log兩個(gè)文件。

stdout.log文件內(nèi)容如下:

log?message?0
log?message?1
log?message?2
log?message?3
log?message?4
log?message?5
log?message?6
log?message?7
log?message?8
log?message?9
log?message?10
...

stderr.log文件內(nèi)容如下:

err?message?0
err?message?1
err?message?2
err?message?3
err?message?4
err?message?5
err?message?6
err?message?7
err?message?8
err?message?9
err?message?10
...

看上去信息還比較簡(jiǎn)單,不像是日志文件的樣子,我們或許得為每條日志添加一個(gè)時(shí)間才行,下面先為Date對(duì)象添加一個(gè)format的原型方法:

//?添加format方法
Date.prototype.format?=?function?(format)?{
??if?(!format)?{
????format?=?'yyyy-MM-dd?HH:mm:ss';
??}
??
??//?用0補(bǔ)齊指定位數(shù)
??let?padNum?=?function?(value,?digits)?{
????return?Array(digits?-?value.toString().length?+?1).join('0')?+?value;
??};
??//?指定格式字符
??let?cfg?=?{
????yyyy:?this.getFullYear(),?????????????//?年
????MM:?padNum(this.getMonth()?+?1,?2),????????//?月
????dd:?padNum(this.getDate(),?2),???????????//?日
????HH:?padNum(this.getHours(),?2),??????????//?時(shí)
????mm:?padNum(this.getMinutes(),?2),?????????//?分
????ss:?padNum(this.getSeconds(),?2),?????????//?秒
????fff:?padNum(this.getMilliseconds(),?3),??????//?毫秒
??};
??return?format.replace(/([a-z]|[A-Z])(\1)*/ig,?function?(m)?{
????return?cfg[m];
??});
}

然后再改寫前面的主文件:

//?index.js
let?fs?=?require('fs');
let?options?=?{
??flags:?'a',?????//?append模式
??encoding:?'utf8',??//?utf8編碼
};
let?stdout?=?fs.createWriteStream('./stdout.log',?options);
let?stderr?=?fs.createWriteStream('./stderr.log',?options);
//?創(chuàng)建logger
let?logger?=?new?console.Console(stdout,?stderr);
//?添加format方法
Date.prototype.format?=?function?(format)?{
??if?(!format)?{
????format?=?'yyyy-MM-dd?HH:mm:ss';
??}
??
??//?用0補(bǔ)齊指定位數(shù)
??let?padNum?=?function?(value,?digits)?{
????return?Array(digits?-?value.toString().length?+?1).join('0')?+?value;
??};
??//?指定格式字符
??let?cfg?=?{
????yyyy:?this.getFullYear(),?????????????//?年
????MM:?padNum(this.getMonth()?+?1,?2),????????//?月
????dd:?padNum(this.getDate(),?2),???????????//?日
????HH:?padNum(this.getHours(),?2),??????????//?時(shí)
????mm:?padNum(this.getMinutes(),?2),?????????//?分
????ss:?padNum(this.getSeconds(),?2),?????????//?秒
????fff:?padNum(this.getMilliseconds(),?3),??????//?毫秒
??};
??return?format.replace(/([a-z]|[A-Z])(\1)*/ig,?function?(m)?{
????return?cfg[m];
??});
}
for?(let?i?=?0;?i?<?100;?i++)?{
??let?time?=?new?Date().format('yyyy-MM-dd?HH:mm:ss.fff');
??logger.log(`[${time}]?-?log?message?${i}`);
??logger.error(`[${time}]?-?err?message?${i}`);
}

重新運(yùn)行程序,然后查看兩個(gè)日志文件的內(nèi)容。

stdout.log內(nèi)容如下:

[2018-04-27?07:30:54.309]?-?log?message?0
[2018-04-27?07:30:54.312]?-?log?message?1
[2018-04-27?07:30:54.312]?-?log?message?2
[2018-04-27?07:30:54.312]?-?log?message?3
[2018-04-27?07:30:54.312]?-?log?message?4
[2018-04-27?07:30:54.312]?-?log?message?5
[2018-04-27?07:30:54.312]?-?log?message?6
[2018-04-27?07:30:54.312]?-?log?message?7
[2018-04-27?07:30:54.312]?-?log?message?8
[2018-04-27?07:30:54.312]?-?log?message?9
[2018-04-27?07:30:54.312]?-?log?message?10
...

stderr.log內(nèi)容如下:

[2018-04-27?07:30:54.309]?-?err?message?0
[2018-04-27?07:30:54.312]?-?err?message?1
[2018-04-27?07:30:54.312]?-?err?message?2
[2018-04-27?07:30:54.312]?-?err?message?3
[2018-04-27?07:30:54.312]?-?err?message?4
[2018-04-27?07:30:54.312]?-?err?message?5
[2018-04-27?07:30:54.312]?-?err?message?6
[2018-04-27?07:30:54.312]?-?err?message?7
[2018-04-27?07:30:54.312]?-?err?message?8
[2018-04-27?07:30:54.312]?-?err?message?9
[2018-04-27?07:30:54.312]?-?err?message?10
...

相信看了本文案例你已經(jīng)掌握了方法,更多精彩請(qǐng)關(guān)注php中文網(wǎng)其它相關(guān)文章!

推薦閱讀:

utils.js使用案例詳解

使用JS怎樣實(shí)現(xiàn)最簡(jiǎn)單的跨域

The above is the detailed content of How to print log information in console. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72
Pre-orders open for new Nintendo Switch Lite refresh Pre-orders open for new Nintendo Switch Lite refresh Jun 29, 2024 am 06:49 AM

Nintendo has opened pre-orders for the latest version of the Switch Lite (curr. $189.99 on Amazon). However, the device is not available to order globally just yet. To recap, the company presented the Switch Lite Hyrule Edition almost two weeks ago d

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

MagicX XU Mini M: Teardown reveals RK3326 CPU instead of advertised RK3562, MagicX severs ties with 3rd-party dev MagicX XU Mini M: Teardown reveals RK3326 CPU instead of advertised RK3562, MagicX severs ties with 3rd-party dev Sep 01, 2024 am 06:30 AM

If you purchased the MagicX XU Mini M recently, this news might come as a surprise. A hardware and software teardown of the newly released handheld console revealed that the advertised RK3562 CPU is, in fact, a lower-specced, older RK3326 processor.

Nintendo announces new Switch Lite refresh before Switch 2 release Nintendo announces new Switch Lite refresh before Switch 2 release Jun 20, 2024 am 09:41 AM

Nintendo presented plenty of games yesterday during its most recent Nintendo Direct event, an overview of which we have provided separately. Additionally, the company also announced a new version of the Switch Lite (curr. $194.93 on Amazon), possibly

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

Nintendo Switch 2 rumored to get ports of Assassin\'s Creed Shadows and previous entries in the series Nintendo Switch 2 rumored to get ports of Assassin\'s Creed Shadows and previous entries in the series Aug 14, 2024 pm 12:36 PM

Last week, Paul Gele, a known gaming insider, shared that the upcoming Nintendo Switch 2 will get major third-party AAA ports at launch. This is something to look forward to because the first-gen gaming handheld didn't get any AAA titles at launch. B

See all articles