Loger.js
1.7 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
// //////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2016-present All Rights Reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// http://www.apache.org/licenses/LICENSE-2.0
//
// Github Home: https://github.com/AlexWang1987
// Author: AlexWang
// Date: 2016-08-27 22:58:47
// QQ Email: 1669499355@qq.com
// Last Modified time: 2017-05-20 11:56:07
// Description: LiveClass-Loger
//
// //////////////////////////////////////////////////////////////////////////////
class Loger {
constructor(info) {
this.info = info || '';
this.id = this.initId();
}
initId() {
const infoType = this.info.constructor.name.toLowerCase();
if (infoType === 'string') {
return this.info;
}
if (infoType === 'object') {
return this.info.mid || '';
}
return '';
}
log(...msg) {
this._log(Loger.LOG, msg);
}
warn(...msg) {
this._log(Loger.WARN, msg);
}
error(...msg) {
this._log(Loger.ERROR, msg);
}
_log(type, msg) {
msg = JSON.stringify(msg);
let logMsg = `${this.id} -> ${msg}`;
if (type >= Loger.logLevel) {
switch (type) {
case Loger.LOG:
console.log(logMsg);
break;
case Loger.WARN:
console.warn(logMsg);
break;
case Loger.ERROR:
console.error(logMsg);
break;
}
}
}
}
Loger.LOG = 0;
Loger.WARN = 1;
Loger.ERROR = 2;
Loger.NO = Infinity;
Loger.logLevel = Loger.LOG;
export default {
getLoger: function getLoger(info) {
return new Loger(info);
},
setLogLevel: function setLogLevel(logLevel) {
Loger.logLevel = logLevel;
},
LOG: Loger.LOG,
WARN: Loger.WARN,
ERROR: Loger.ERROR,
NO: Loger.NO,
};