This repository has been archived by the owner on Jan 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.log.js
executable file
·79 lines (64 loc) · 1.71 KB
/
util.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
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
/**
* 日志工具类
* 用法:
* 引入
* const logger = require('util.log').getLogger(name);
* 强制关闭(无视config里的日志等级):logger.off();
* 强制打开(无视config里的日志等级):logger.on();
* 取消强制设定:logger.reset();
* 使用
* logger.info("a");
* logger.warn("a","b");
*/
const CONFIG = require('config')
class Logger{
constructor(){
this.LEVEL = new Map([["DEBUG",1],["INFO",2],["WARN",3],["ERROR",4],["OFF",5]])
this.logSettingNum = 5;
this.forceControl=null;
this.name = "";
}
static getLogger(name){
let logger = new Logger();
logger.name = name;
let num = logger.LEVEL.get(CONFIG.LOGGER_LEVEL);
num ? logger.logSettingNum=num:logger.logSettingNum=5;
return logger;
}
_canLog(levelNum){
if(levelNum==null) return false;
return this.forceControl==true||levelNum>=this.logSettingNum&&this.forceControl==null
}
_log(level,messages){
if(this._canLog(this.LEVEL.get(level.trim()))){
console.log("["+level+"]["+this.name+"]",messages.join(" "));
}
}
off(){
this.forceControl=false;
return this;
}
on(){
this.forceControl=true;
return this;
}
reset(){
this.forceControl=null;
return this;
}
debug(...message){
this._log("DEBUG",message)
}
info(...message){
this._log("INFO ",message)
}
warn(...message){
this._log("WARN ",message)
}
error(...message){
this._log("ERROR",message)
}
inject(fn){
}
}
module.exports = Logger;