-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.js
116 lines (83 loc) · 1.97 KB
/
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
* 简单日志模块
*
* 后续要实现在IE下可以更加方便友好地显示日志,并且方便调试
* 提供方便快捷的方式来过滤日志
*
* 当然最好能和后台结合,把出错信息记录到后台,以方便前端异常的监控
*
* @author qijun.weiqj
*/
define('Log', ['jQuery', 'Class'],
function($, Class) {
var Log = new Class({
init: function(name) {
this.name = name;
},
error: function(message) {
this.log(message, 'error');
},
warn: function(message) {
this.log(message, 'warn');
},
info: function(message) {
this.log(message, 'info');
},
log: function(message, level) {
simpleLog(message, level, this.name);
},
isEnabled: may.log.isEnabled
});
//~ Log
var body = null,
list = [],
search = window.location.search,
logConsole = /\bdebug-log-console=true\b/.test(search),
filter = (/\bdebug-log-filter=([^&]+)/.exec(search) || {})[1];
var prepare = function() {
var container = $('<div class="debug-container"></div>').appendTo('body'),
clear = $('<a class="clear" href="#">Clear</a>').appendTo(container);
body = $('<div class="body"></div>').appendTo(container);
clear.on('click', function(e) {
e.preventDefault();
body.empty();
});
$.each(list, function(index, message) {
body.append(message);
});
};
logConsole && $(function() {
prepare();
});
var oriLog = may.log.handler;
var simpleLog = function(message, level, name) {
level = level || 'info'
if (!may.log.isEnabled(level)) {
return;
}
if (filter && !checkFilter(message, level, name)) {
return;
}
if (logConsole) {
var node = $('<p class="debug debug-' + level + '"></p>');
node.text((name ? '[' + name + '] ' : '') + message);
if (body) {
body.append(node);
} else {
list.push(node);
}
} else {
oriLog(message, level, name);
}
};
var checkFilter = function(message, level, name) {
if (name && name.indexOf(filter) !== -1 ||
level === filter ||
message && message.indexOf(filter) !== -1) {
return true;
}
return false;
};
may.log.handler = simpleLog;
return Log;
});