How to print log information in console
Jun 04, 2018 pm 03:16 PM這次給大家?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)文章!
推薦閱讀:
使用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!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

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

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

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

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

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
