-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlog.js
More file actions
127 lines (114 loc) · 3.76 KB
/
log.js
File metadata and controls
127 lines (114 loc) · 3.76 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
'use strict';
const litelog = require('litelog');
//获取打印日志的位置
function getPos(fix) {
var stack = new Error().stack.split('\n');
var line = stack[fix];
var lastSpace = line.lastIndexOf(' ');
line = line.substring(lastSpace + 1, line.length);
if (line[0] === '(') {
line = line.substring(1, line.length - 1);
}
return line;
}
var mockLog = {
_called: false,
messageInfoCache: [],
logLevels: ['debug', 'trace', 'info', 'warn', 'error', 'fatal'],
logInstance: {},
get: function (name) {
name = name || 'sys';
if (!this.logInstance[name]) {
this.logInstance[name] = {
_log: (...args) => {
this.messageInfoCache.push(args);
}
};
this.logLevels.forEach((level) => {
this.logInstance[name][level] = function (...args) {
//获取 log 的打印位置,和打印的内容
var pos = getPos(3);
args.unshift(litelog.getTime());
this._log(name, level.toUpperCase(), args, pos);
};
});
// alias
this.logInstance[name].log = this.logInstance[name].info;
this.logInstance[name].err = this.logInstance[name].error;
this.logInstance[name].warning = this.logInstance[name].warn;
// bind(this), 所有的 get 返回的都是 logInstance的成员
this.logInstance[name].get = this.get.bind(this);
}
return this.logInstance[name];
}
};
function exit() {
mockLog.messageInfoCache.forEach(function (v) {
let time = '';
if (Array.isArray(v[2])) {
time = v[2].shift();
v[2] = v[2].map(v => {
if (typeof v === 'object') {
return JSON.stringify(v);
} else {
return v;
}
}).join(' ');
}
console.log.apply(console, [time, '[' + v[1] + ']', v[0], v[2], '(' + v[3] + ')']);
});
}
process.on('exit', exit);
//返回的是一个 function,根据 name 返回 log对象
var mockLogFunc = function (name) {
return mockLog.get(name);
};
//无需执行,mockLogFunc就和 mockLogFunc.get('sys')有一样的方法
mockLogFunc.__proto__ = mockLog.get('sys');
module.exports = mockLogFunc;
module.exports.init = (opt, debug) => {
litelog.Logger.prototype.log = litelog.Logger.prototype.info;
litelog.Logger.prototype.err = litelog.Logger.prototype.error;
litelog.Logger.prototype.warning = litelog.Logger.prototype.warn;
let log = litelog.create(opt);
if (log.colorful) {
log.colorful(debug);
}
if (!log.debug || !log.info || !log.warn || !log.error) {
throw new Error('log should have method: debug, info, warn, error');
}
//初始化完毕后调用真正的 log 对象打印之前保存的 log 信息
if (mockLog.messageInfoCache.length > 0) {
mockLog.messageInfoCache.forEach((messageInfo) => {
//messageInfo[0]是 log 对象的名字,如果实际的 log对象中没有,就是 sys
if (Array.isArray(messageInfo[2])) {
messageInfo[2].shift();
}
log.get(messageInfo[0])._log(messageInfo[1].toUpperCase(), messageInfo[2], messageInfo[3]);
});
// clean cache info
mockLog.messageInfoCache = [];
}
process.removeListener('exit', exit);
//如果存在初始化完成之前的 log 对象,需要把这些对象的所有方法替换成真正的 log 对象的方法
Object.keys(mockLog.logInstance).forEach((name) => {
let oldLog = mockLog.logInstance[name];
let newLog = log.get(name);
mockLog.logLevels.forEach((level) => {
oldLog[level] = (...args) => {
newLog._log(level, args);
}
});
});
module.exports = (name) => {
if (name) {
return log.get(name);
}
return log;
};
module.exports.__proto__ = log;
module.exports.init = () => {
throw new Error('log has been initialized, can\'t be initialized again.');
};
return log;
};