-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlog.js
46 lines (37 loc) · 1010 Bytes
/
log.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const moment = require('moment');
class Log {
constructor() {
this.verbose_ = true;
}
error(who, ...what) {
console.error(this._msg('ERR', who, ...what));
}
log(who, ...what) {
console.log(this._msg('LOG', who, what));
}
info(who, ...what) {
console.info(this._msg('INF', who, what));
}
verbose(who, ...what) {
this.verbose_ && console.log(this._msg('VER', who, what));
}
setVerbose(value) {
this.verbose_ = value;
}
_msg(type, who, ...[what]) {
let str = '';
what.forEach(item => {
if (typeof item === 'object') {
str += JSON.stringify(item) + ' ';
} else {
str += item.toString() + ' ';
}
});
const pre = `${moment().format('YYYY-MM-DD hh:mm:ss')} [${who.constructor.name}] ${type.toUpperCase()} > `.padEnd(55, ' ');
return pre + str;
}
}
module.exports = {
Log,
log: new Log
};