-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnlog.js
More file actions
137 lines (121 loc) · 3.81 KB
/
nlog.js
File metadata and controls
137 lines (121 loc) · 3.81 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//joffe 流方式日志模块
const fs = require('fs');
const path = require('path');
let curday = new Date().getDate();
const BUFFER_CHECK_INTERVAL = 2000;//2s
const BUFFER_FLUSH_LEN = 4096;
const env = process.env.env||'local';
const LOG_DIRECTORY = path.resolve(`./logs-${env}/`);
let dnow = new MyDate();
setInterval(()=>{
dnow = new MyDate();
},800)
function MyDate(){
let date = new Date();
date.formatMap = {};
date.getFromFormat = function(format){
if(!date.formatMap[format])
date.formatMap[format] = date.Format(format)
return date.formatMap[format];
}
return date;
}
MyDate.prototype.Format = function (fmt) {
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
//logFile 类.用流来写入log 更高效,避免爆仓
//http://segmentfault.com/blog/chshouyu/1190000000519006
function LogFile(path){
this.buffers = [];
this.bufferCheckInterval = BUFFER_CHECK_INTERVAL;
this.init(path);
}
LogFile.prototype.init = function(path) {
let self = this;
this.path = path;
this.stream = fs.createWriteStream(path, {
flags : 'a' //全部权限,读写
});
this.bufferCheckTimer = setInterval(function() {
self._flush(); //定时保存
}, this.bufferCheckInterval);
};
LogFile.prototype.destroy = function() {
this._flush();
if (this.bufferCheckTimer) {
clearInterval(this.bufferCheckTimer);
this.bufferCheckTimer = null;
}
if (this.stream) {
this.stream.end();
this.stream.destroySoon();
this.stream = null;
}
};
LogFile.prototype.restart = function(path) {
this.destroy();
this.init(path);
};
LogFile.prototype.push = function(str) {
this.buffers.push(str);
if (this.buffers.length >= BUFFER_FLUSH_LEN) {
this._flush();
}
};
LogFile.prototype._flush = function() {
if (this.buffers.length > 0 && this.stream) {
this.buffers.push('');
let str = this.buffers.join('\n');
this.stream.write(str);
this.buffers = [];
}
};
function format(str,not_need_time){
if(not_need_time){
return (str+'').replace(/[\r\n]/g,"##");
}else{
return dnow.getFromFormat('YYYY-MM-dd hh:mm:ss') +'\t' + str.replace(/[\r\n]/g,"##");
}
}
function mkdirs(file_path){
if(fs.existsSync(file_path))
return true;
mkdirs(path.dirname(file_path))
fs.mkdirSync(file_path)
}
/**
* @param type String
* @param timeFormat String "yyyy-MM-dd hh:mm:ss"
* @param options Object {extname:"",debug:false,not_need_time:false}
*/
exports.register = function(type,timeFormat,options){
options = options||{};
if(exports[type])
return;
if(!type||!timeFormat)
throw "type and timeFormat is required "
let extname = options.extname||`.${type}.log`;
let dir = path.resolve(LOG_DIRECTORY,type);
mkdirs(dir);
let log = new LogFile(path.resolve(dir,dnow.getFromFormat(timeFormat))+extname)
exports[type] = function(str){
str = str.toString();
if(options.debug)
console.log(str);
if(log.path!=dnow.getFromFormat(timeFormat))
log.restart(path.resolve(dir,dnow.getFromFormat(timeFormat))+extname)
log.push(format(str,options.not_need_time))
}
}