这次给大家带来console怎样打印日志信息,console打印日志信息的注意事项有哪些,下面就是实战案例,一起来看一下。
我们首先创建如下文件:
// 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); // 创建logger let logger = new console.Console(stdout, stderr); for (let i = 0; i <p style="text-align: left;">在上面代码中,我们其实是创建了一个console.Console类的实例,该类需要指定两个参数,即标准输出流和标准错误输出流,正常情况下,实际上是对应了process.stdout和process.stderr,以上的代码中,我们将这两个输出流改为了文件输出流,并指定为文件追加模式,这样即可将日志信息输出到指定的文件中去。运行上面的代码,会生成stdout.log和stderr.log两个文件。</p><p style="text-align: left;">stdout.log文件内容如下:</p><pre class="brush:php;toolbar:false">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文件内容如下:
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 ...
看上去信息还比较简单,不像是日志文件的样子,我们或许得为每条日志添加一个时间才行,下面先为Date对象添加一个format的原型方法:
// 添加format方法 Date.prototype.format = function (format) { if (!format) { format = 'yyyy-MM-dd HH:mm:ss'; } // 用0补齐指定位数 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), // 时 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); // 创建logger let logger = new console.Console(stdout, stderr); // 添加format方法 Date.prototype.format = function (format) { if (!format) { format = 'yyyy-MM-dd HH:mm:ss'; } // 用0补齐指定位数 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), // 时 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 <p style="text-align: left;">重新运行程序,然后查看两个日志文件的内容。</p><p style="text-align: left;">stdout.log内容如下:</p><pre class="brush:php;toolbar:false">[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内容如下:
[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 ...
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
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

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

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

Undress AI Tool
Undress images for free

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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

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