-
Notifications
You must be signed in to change notification settings - Fork 0
/
syncLogerr.js
365 lines (318 loc) · 12.4 KB
/
syncLogerr.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/**
* sync logerr
*
* @category syncLogerr
* @author Yuri Rynkov <yuri@rynkov.eu>
* @copyright Copyright (c) 2018 Yuri Rynkov <https://github.com/deepq/syncLogger>
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version 0.1
* @description Based on original code of 'logger' <https://github.com/i-break-codes/logerr> by Vaibhav Mehta <firekillz@gmail.com>
*/
var Logerr = function () {
'use strict';
var setConfig;
var failedErrors = [];
var browser = detect();
function init(userConfig) {
if (!userConfig) userConfig = {};
// Default configuration
var config = {
detailedErrors: true,
remoteLogging: false,
syncInterval: 5000,
remoteSettings: {
url: null,
additionalParams: null,
successCallback: null,
errorCallback: null
}
};
// Override with user config
setConfig = Object.assign(config, userConfig);
//Remove current listener
window.removeEventListener('error', listener);
// Listen to errors
window.addEventListener('error', listener);
syncFailedErrors(setConfig);
}
// NOTE: Private
function listener(e) {
if (setConfig.detailedErrors) {
detailedErrors(e);
}
if (setConfig.remoteLogging) {
remoteLogging(e, setConfig.remoteSettings);
}
}
function detailedErrors(e) {
var i = errorData(e);
var str = [
"%cType: %c" + i.type,
"%cError: %c" + i.error,
"%cStackTrace: %c" + i.stackTrace,
"%cFile Name: %c" + i.filename,
"%cPath: %c" + i.path,
"%cLine: %c" + i.line,
"%cColumn: %c" + i.column,
"%cDate: %c" + i.datetime,
"%cDebug: %c" + i.path + ':' + i.line
].join("\n");
if (window.chrome) {
console.log(str, "font-weight: bold;", "color: #e74c3c;", "font-weight: bold;", "font-weight: normal; color: #e74c3c;", "font-weight: bold;", "font-weight: normal; color: #e74c3c;", "font-weight: bold;", "font-weight: normal;", "font-weight: bold;", "font-weight: normal;", "font-weight: bold;", "font-weight: normal;", "font-weight: bold;", "font-weight: normal;", "font-weight: bold;", "font-weight: normal;", "font-weight: bold;", "font-weight: normal;", "font-weight: bold;", "font-weight: normal; color: #3498db;");
} else {
console.log(str.replace(/%c/gi, ''));
}
}
function makeRequest(url, data, successCb, failCb) {
var http = new XMLHttpRequest();
http.open("POST", url, true);
http.setRequestHeader("Content-type", "application/json");
http.send(JSON.stringify(data));
http.onreadystatechange = function () {
if (http.readyState === 4 && http.status === 200) {
successCb && successCb();
}
if (http.readyState === 4 && http.status !== 200) {
failCb && failCb();
}
};
}
function remoteLogging(e, remoteSettings) {
if (!remoteSettings.url) {
throw new Error('Provide remote URL to log errors remotely');
} else if (remoteSettings.additionalParams && typeof remoteSettings.additionalParams !== 'object') {
throw new Error('Invalid data type, additionalParams should be a valid object');
}
var data = errorData(e);
var setData = Object.assign(data, remoteSettings.additionalParams);
function handleSuccess() {
remoteSettings.successCallback
&& typeof remoteSettings.successCallback === 'function'
&& remoteSettings.successCallback()
}
function handleError() {
remoteSettings.errorCallback
&& typeof remoteSettings.errorCallback === 'function'
&& remoteSettings.errorCallback();
failedErrors.push(e);
}
makeRequest(remoteSettings.url, setData, handleSuccess, handleError);
}
function remoteLogBatch(batch, remoteSettings, successCb, errorCb) {
if (!remoteSettings.batchUrl) {
throw new Error('Provide remote batch URL to log errors remotely');
} else if (remoteSettings.additionalParams && typeof remoteSettings.additionalParams !== 'object') {
throw new Error('Invalid data type, additionalParams should be a valid object');
}
var data = batch.map(function (e) {
return Object.assign(errorData(e), remoteSettings.additionalParams);
});
function handleSuccess() {
successCb && typeof successCb === 'function' && successCb();
}
function handleError() {
errorCb && typeof errorCb === 'function' && errorCb();
}
makeRequest(remoteSettings.batchUrl, {events: data}, handleSuccess, handleError);
}
function syncFailedErrors(config) {
function handleSuccess() {
failedErrors = [];
}
setInterval(function () {
if (failedErrors.length > 0) {
remoteLogBatch(failedErrors, setConfig.remoteSettings, handleSuccess)
}
}, config.syncInterval);
}
function log(data) {
if (setConfig.remoteLogging) {
remoteLogging(data, setConfig.remoteSettings);
}
}
function errorData(e) {
var datetime = new Date().toString();
/**
* userAgent only for POST request purposes, not required in pretty print
*/
if (e instanceof ErrorEvent) {
var filename = e.filename.lastIndexOf('/');
return {
type: e.type,
path: e.filename,
filename: e.filename.substring(++filename),
line: e.lineno,
column: e.colno,
error: e.message,
stackTrace: ((e.error) ? e.error.stack.toString().replace(/(\r\n|\n|\r)/gm, "") : ""),
datetime: datetime,
dateutc: Date.now(),
userAgent: navigator.userAgent || window.navigator.userAgent,
browser: browser,
language: navigator.language,
screen: getScreen(),
url: window.document.URL,
queryString: window.location.search,
location: window.location.hash,
codeVersion: '',
logLevel: 'error',
duration: e.duration || '',
code: e.code || '',
metadata: (typeof e.metadata === 'object') ? JSON.stringify(e.metadata) : e.metadata
};
} else {
return {
type: e.type || 'log',
path: '',
filename: '',
line: '',
column: '',
error: e.message,
stackTrace: '',
datetime: datetime,
dateutc: Date.now(),
userAgent: navigator.userAgent || window.navigator.userAgent,
browser: browser,
language: navigator.language,
screen: getScreen(),
url: window.document.URL,
queryString: window.location.search,
location: window.location.hash,
codeVersion: '',
logLevel: e.logLevel || 'info',
duration: e.duration || '',
code: e.code || '',
metadata: (typeof e.metadata === 'object') ? JSON.stringify(e.metadata) : e.metadata
}
}
}
//Polyfill for Object.assign
if (typeof Object.assign !== 'function') {
Object.assign = function (target) {
if (target === null) {
throw new TypeError('Cannot convert undefined or null to object');
}
target = Object(target);
for (var index = 1; index < arguments.length; index++) {
var source = arguments[index];
if (source !== null) {
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
}
return target;
};
}
//screen detection
function getScreen() {
var screen = window.screen;
return {
height: screen.height,
width: screen.width,
pixelDepth: screen.pixelDepth,
colorDepth: screen.colorDepth,
orientation: screen.orientation ? screen.orientation.type : ''
}
}
//browser detection
function detect() {
if (typeof navigator !== 'undefined') {
return parseUserAgent(navigator.userAgent);
}
return null;
}
function detectOS(userAgentString) {
var rules = getOperatingSystemRules();
var detected = rules.filter(function (os) {
return os.rule && os.rule.test(userAgentString);
})[0];
return detected ? detected.name : null;
}
function parseUserAgent(userAgentString) {
var browsers = getBrowserRules();
if (!userAgentString) {
return null;
}
var detected = browsers.map(function (browser) {
var match = browser.rule.exec(userAgentString);
var version = match && match[1].split(/[._]/).slice(0, 3);
if (version && version.length < 3) {
version = version.concat(version.length === 1 ? [0, 0] : [0]);
}
return match && {
name: browser.name,
version: version.join('.')
};
}).filter(Boolean)[0] || null;
if (detected) {
detected.os = detectOS(userAgentString);
}
return detected;
}
function getBrowserRules() {
return buildRules([
['edge', /Edge\/([0-9\._]+)/],
['yandexbrowser', /YaBrowser\/([0-9\._]+)/],
['vivaldi', /Vivaldi\/([0-9\.]+)/],
['kakaotalk', /KAKAOTALK\s([0-9\.]+)/],
['chrome', /(?!Chrom.*OPR)Chrom(?:e|ium)\/([0-9\.]+)(:?\s|$)/],
['phantomjs', /PhantomJS\/([0-9\.]+)(:?\s|$)/],
['crios', /CriOS\/([0-9\.]+)(:?\s|$)/],
['firefox', /Firefox\/([0-9\.]+)(?:\s|$)/],
['fxios', /FxiOS\/([0-9\.]+)/],
['opera', /Opera\/([0-9\.]+)(?:\s|$)/],
['opera', /OPR\/([0-9\.]+)(:?\s|$)$/],
['ie', /Trident\/7\.0.*rv\:([0-9\.]+).*\).*Gecko$/],
['ie', /MSIE\s([0-9\.]+);.*Trident\/[4-7].0/],
['ie', /MSIE\s(7\.0)/],
['bb10', /BB10;\sTouch.*Version\/([0-9\.]+)/],
['android', /Android\s([0-9\.]+)/],
['ios', /Version\/([0-9\._]+).*Mobile.*Safari.*/],
['safari', /Version\/([0-9\._]+).*Safari/]
]);
}
function getOperatingSystemRules() {
return buildRules([
['iOS', /iP(hone|od|ad)/],
['Android OS', /Android/],
['BlackBerry OS', /BlackBerry|BB10/],
['Windows Mobile', /IEMobile/],
['Amazon OS', /Kindle/],
['Windows 3.11', /Win16/],
['Windows 95', /(Windows 95)|(Win95)|(Windows_95)/],
['Windows 98', /(Windows 98)|(Win98)/],
['Windows 2000', /(Windows NT 5.0)|(Windows 2000)/],
['Windows XP', /(Windows NT 5.1)|(Windows XP)/],
['Windows Server 2003', /(Windows NT 5.2)/],
['Windows Vista', /(Windows NT 6.0)/],
['Windows 7', /(Windows NT 6.1)/],
['Windows 8', /(Windows NT 6.2)/],
['Windows 8.1', /(Windows NT 6.3)/],
['Windows 10', /(Windows NT 10.0)/],
['Windows ME', /Windows ME/],
['Open BSD', /OpenBSD/],
['Sun OS', /SunOS/],
['Linux', /(Linux)|(X11)/],
['Mac OS', /(Mac_PowerPC)|(Macintosh)/],
['QNX', /QNX/],
['BeOS', /BeOS/],
['OS/2', /OS\/2/],
['Search Bot', /(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves\/Teoma)|(ia_archiver)/]
]);
}
function buildRules(ruleTuples) {
return ruleTuples.map(function (tuple) {
return {
name: tuple[0],
rule: tuple[1]
};
});
}
return {
init: init,
log: log
};
}();