-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlog.js
194 lines (161 loc) · 5.47 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
* LogJS (c)2013 Brett Fattori
* Lightweight JavaScript logging framework
* MIT Licensed
*/
(function(global, undefined){
var LogJS = {
EXCEPTION: 'EXCEPTION',
ERROR: 'ERROR',
WARN: 'WARN',
INFO: 'INFO',
version: 'LogJS v1.2.2',
window_: global
};
var appenders = {};
// This is the method for logging. It passes off to the
// appenders to perform the actual logging.
var log = function (type, message, url, lineNumber) {
var now = new Date().getTime();
if (message instanceof Error) {
if (message.stack) {
message = (message.message && message.stack.indexOf(message.message) === -1) ?
message.message + "\n" + message.stack :
message.stack;
} else if (message.sourceURL) {
message = message.message;
url = message.sourceURL;
lineNumber = message.line;
}
}
for (var appender in appenders) {
if (appenders.hasOwnProperty(appender)) {
appenders[appender].log(type, now, message, url, lineNumber);
}
}
};
// Redirect the onerror handler for the global object (if it exists)
var win = LogJS.window_;
var gErrorHandler;
if (win.onerror !== undefined) {
gErrorHandler = win.onerror;
}
win.onerror = function onErrorLogJS(message, url, lineNumber) {
log(LogJS.EXCEPTION, message, url, lineNumber);
if (gErrorHandler) {
gErrorHandler(message, url, lineNumber);
}
};
// --------------------------------------------------------------------------------------------------
LogJS.error = function(message, url, lineNumber) {
log(LogJS.ERROR, message, url, lineNumber);
};
LogJS.warn = function(message, url, lineNumber) {
log(LogJS.WARN, message, url, lineNumber);
};
LogJS.info = function(message, url, lineNumber) {
log(LogJS.INFO, message, url, lineNumber);
};
// --------------------------------------------------------------------------------------------------
LogJS.addAppender = function(appender) {
if (appender !== undefined) {
appender = new appender(LogJS.config);
if (appender.LOGJSAPPENDER) {
appenders[appender.name] = appender;
}
}
};
LogJS.getAppender = function(appenderName) {
return appenders[appenderName];
};
LogJS.getRegisteredAppenders = function() {
var registered = [];
for (var appender in appenders) {
if (appenders.hasOwnProperty(appender)) {
registered.push(appender);
}
}
return registered;
};
Object.defineProperty(LogJS, 'config', {
configurable: false,
value: {},
writable: true,
enumerable: false
});
// --------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------
LogJS.BaseAppender = function() {
};
Object.defineProperty(LogJS.BaseAppender.prototype, 'LOGJSAPPENDER', {
configurable: false,
value: true,
writable: false,
enumerable: false
});
LogJS.BaseAppender.prototype.log = function(type, message, url, lineNumber) {
};
LogJS.BaseAppender.prototype.configOpt = function(key, config, optValue) {
return (config[this.name] && config[this.name][key]) || optValue;
};
Object.defineProperty(LogJS.BaseAppender.prototype, 'name', {
configurable: false,
value: 'LogJSBaseAppender',
writable: true,
enumerable: false
});
// Angular
if (typeof angular !== 'undefined') {
LogJS.config.global = {
debug: true
};
function LogJSProvider() {
this.config = LogJS.config;
var self = this;
this.debugEnabled = function(flag) {
if (typeof flag !== 'undefined') {
this.config.global = {
debug: flag
};
return this;
} else {
return this.config.global.debug;
}
};
this.$get = function() {
var angularLogJS = {};
['error','info','debug','log','warn'].forEach(function(e) {
var method = LogJS.info;
if (LogJS[e]) {
method = LogJS[e];
}
angularLogJS[e] = (function(method, e) {
return function() {
if (e !== 'debug' || self.config.global.debug) {
method.apply(LogJS, arguments);
}
}
})(method, e);
});
return angularLogJS;
};
}
angular.module('ng').provider('$log', LogJSProvider);
}
// Exports
// -------
// AMD
if (typeof define !== 'undefined' && define.amd) {
define([], function () {
return LogJS;
});
}
// CommonJS
else if (typeof module !== 'undefined' && module.exports) {
module.exports = LogJS;
}
// Script tag
else {
global.LogJS = LogJS;
}
})(this);