Usually when we write the node. JS program, we are accustomed to use Console.log print log information, but this is limited to the console output, sometimes we need to output information to the log file, in fact, using the console can also achieve this goal, today to briefly introduce.
We first create the following file:
//Index.js LetFs= require(' FS '); LetOptions= { Flags: ' A ', //Append mode encoding: ' UTF8 ', //UTF8 encoding}; LetStdOut= FS.Createwritestream('./stdout.log ',Options; LetStdErr= FS.Createwritestream('./stderr.log ',Options;//Create Logger LetLogger= New Console.Console(stdout,StdErr; for( LetI= 0;I< -;I++){ Logger.Log(' Log Message${I}`); Logger.Error(' Err message${I}`);}
In the above code, we are actually creating a console. An instance of the console class that needs to specify two parameters, the standard output stream and the standard error output stream, which, under normal circumstances, Actually corresponds to the process.stdout and Process.stderr, in the above code, we changed the two output streams to the file output stream, and specified as the file append mode, so that the log information can be exported to the specified file. Running the above code will generate two files for Stdout.log and Stderr.log.
The contents of the Stdout.log file are as follows:
log message 0log message 1log message 2log message 3log message 4log message 5log message 6log message 7log message 8log message 9log message 10...
The contents of the Stderr.log file are as follows:
err message 0err message 1err message 2err message 3err message 4err message 5err message 6err message 7err message 8err message 9err message 10...
It seems that the information is relatively simple, not like a log file, we may have to add a time for each log, the following first to add a format for the date object of the prototype method:
//Add the Format methodDate.prototype.format = function(format){ if(!Format{Format= ' Yyyy-mm-dd HH:mm:ss '; } //Use 0 to specify the number of digits LetPadnum= function(Value,Digits{ return Array(Digits- value.toString().length + 1).Join(' 0 ')+Value; }; //Specify format characters LetCfg= { yyyy: This.getFullYear(), //year MM: Padnum( This.GetMonth()+ 1, 2), //month DD: Padnum( This.getDate(), 2), //day HH: Padnum( This.getHours(), 2), //Time mm: Padnum( This.getminutes(), 2), //min SS: Padnum( This.getseconds(), 2), //Sec FFF: Padnum( This.getmilliseconds(), 3), //MS }; return format.Replace(/([a-z]|[ A-z]) (\1) */ig, function(m){ returnCFG[M]; });}
Then overwrite the previous main file:
Index.jslet fs = require (' FS '); let options = {flags: ' A ',//Append mode encoding: ' UTF8 ',//UTF8 code};let stdout = Fs.createwritestream ('./stdout.log ', options); let stderr = Fs.createwritestream ('./stderr.log ', options);// Create Loggerlet logger = new console. Console (stdout, stderr);//Add the Format method Date.prototype.format = function (format) {if (!format) {format = ' yyyy-m M-dd HH:mm:ss '; }//With 0 completion specify the number of digits let Padnum = function (value, digits) {return Array (digits-value.tostring (). length + 1). Join (' 0 ') + value; }; Specify format character Let cfg = {yyyy:this.getFullYear (),//year Mm:padnum (This.getmonth () + 1, 2),//month Dd:padnum (This.getdate (), 2),//day Hh:padnum (this.gethour S (), 2),//When Mm:padnum (This.getminutes (), 2),//Sub-ss:padnum (this.get Seconds (), 2),//sec fff:padnum (This.getmillisEconds (), 3),//msec}; Return Format.replace (/([a-z]|[ A-z]) (\1) */ig, function (m) {return cfg[m]; });} for (Let i = 0; i <, 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} ');}
Rerun the program, and then view the contents of the two log files.
Stdout.log content is as follows:
[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 content is as follows:
[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...
Such a simple log output is complete.
Resources:
Https://nodejs.org/api/console.html
Node. JS Series article: Using the console output log file