UNPKG

1.79 kBJavaScriptView Raw
1
2function Logger(options) {
3 //默认日志是关闭的
4 this.level = options.level||Logger.LOG_OFF;
5 this.appendix = options.appendix||"Lighting";
6}
7
8Logger.LEVEL_DEBUG=1;
9Logger.LEVEL_INFO=2;
10Logger.LEVEL_WARN=3;
11Logger.LEVEL_ERROR=4;
12Logger.LOG_OFF=5;
13
14/**
15 * 错误信息
16 * @param msg
17 */
18Logger.prototype.error = function (msg) {
19 if(this.level<=Logger.LEVEL_ERROR){
20 console.error("["+this.appendix+"]"+msg);
21 if(Logger.websocket) Logger.websocket.send(JSON.stringify({
22 type:"log",
23 level:"error",
24 message:msg,
25 userAgent:navigator.userAgent
26 }));
27 }
28};
29
30/**
31 * 警告信息
32 * @param msg
33 */
34Logger.prototype.warn = function (msg) {
35 if(this.level<=Logger.LEVEL_WARN){
36 console.warn("["+this.appendix+"]"+msg);
37 if(Logger.websocket) Logger.websocket.send(JSON.stringify({
38 type:"log",
39 level:"warn",
40 message:msg,
41 userAgent:navigator.userAgent
42 }));
43 }
44};
45
46/**
47 * 一般信息
48 * @param msg
49 */
50Logger.prototype.info = function (msg) {
51 if(this.level<=Logger.LEVEL_INFO){
52 console.info("["+this.appendix+"]"+msg);
53 if(Logger.websocket) Logger.websocket.send(JSON.stringify({
54 type:"log",
55 level:"info",
56 message:msg,
57 userAgent:navigator.userAgent
58 }));
59 }
60};
61
62/**
63 * 调试信息
64 * @param msg
65 */
66Logger.prototype.debug = function (msg) {
67 if(this.level<=Logger.LEVEL_DEBUG){
68 console.log("["+this.appendix+"]"+msg);
69 if(Logger.websocket) Logger.websocket.send(JSON.stringify({
70 type:"log",
71 level:"debug",
72 message:msg,
73 userAgent:navigator.userAgent
74 }));
75 }
76};
77
78export default Logger
\No newline at end of file