Loger.js
2.2 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
// //////////////////////////////////////////////////////////////////////////////
//
// 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: 2016-08-27 23:05:53
// Description: LiveClass-Loger
//
// //////////////////////////////////////////////////////////////////////////////
class Loger {
constructor(info) {
this.sdkInfo = info || '';
this.id = this.initId();
}
initId() {
if(!this.sdkInfo){
console.log("this.sdkInfo无效-->",this.sdkInfo);
return '';
}
const infoType = Object.prototype.toString.call(this.sdkInfo);
if (infoType === '[object String]') {
return this.sdkInfo;
}
if (infoType === '[object Object]') {
return this.sdkInfo.mid || '';
}
return '';
/* const infoType = this.sdkInfo.constructor.name.toLowerCase();
if (infoType === 'string') {
return this.sdkInfo;
}
if (infoType === 'object') {
return this.sdkInfo.mid || '';
}
return '';*/
}
log(...msg) {
this._log(Loger.LOG, msg);
}
warn(...msg) {
this._log(Loger.WARN, msg);
}
error(...msg) {
this._log(Loger.ERROR, msg);
}
data(...msg) {
this._log(Loger.DATA, 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;
case Loger.DATA:
console.log(logMsg);
break;
}
}
}
}
Loger.LOG = 0;
Loger.WARN = 1;
Loger.ERROR = 2;
Loger.NO = Infinity;
Loger.logLevel = Loger.LOG;
Loger.DATA = 5;
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,
DATA: Loger.DATA,
};