-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.js
28 lines (28 loc) · 951 Bytes
/
logger.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
const fs = require( 'fs' ) ;
const path = require( 'path' ) ;
const stream = require( 'stream' ) ;
/* 获取当前时间的格式化 */
function getFormateDate(){
var date = new Date() ,
year = date.getFullYear() ,
month = date.getMonth() + 1 ,
mdate = date.getDate() ,
h = date.getHours() ,
m = date.getMinutes() ,
s = date.getSeconds() ,
ss = date.getMilliseconds() ;
return year + '-' + month + '-' + mdate + ' ' + h + ':' + m + ':' + s + ' ' + ss ;
}
module.exports = function( filePath ){
filePath = path.normalize( filePath ) ;
const newStdout = new stream.PassThrough() ;
newStdout.pipe( fs.createWriteStream( filePath , { 'flags' : 'a' } ) ) ;
return function( str ){
var time = getFormateDate() ;
try{
newStdout.write( time + ' ' + str + '\n' ) ;
}catch(e){
console.warn( '日志写入错误' ) ;
}
}
}