-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.js
102 lines (87 loc) · 2.26 KB
/
logger.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
function wikiaJWPlayerLogger(options) {
var servicesDomain = options.servicesDomain || 'services.wikia.com',
loggerPath = '/event-logger/',
loggerUrl = 'https://' + servicesDomain + loggerPath,
prefix = 'JWPlayer',
logLevels = {
debug: 0,
info: 1,
warn: 2,
error: 3,
off: 4
},
loggerOptions = options.logger || {},
logLevel = loggerOptions.logLevel ? logLevels[loggerOptions.logLevel] : logLevels['error'],
logDebugToService = loggerOptions.logDebugToService,
clientName = loggerOptions.clientName,
clientVersion = loggerOptions.clientVersion;
/**
* logs errors to event-logger service
*
* @param resource - 'debug' or 'error'
* @param name
* @param description
*/
function logToService(resource, name, description) {
var request = new XMLHttpRequest(),
data = {
name: prefix + ' ' + name
};
if (description) {
data.description = typeof description === 'string' ? description : JSON.stringify(description);
}
if (clientName) {
data.client = clientName;
}
if (clientVersion) {
data.client_version = clientVersion;
}
request.open('POST', loggerUrl + resource, true);
request.setRequestHeader('Content-type', 'application/json');
request.send(JSON.stringify(data));
}
function debug(name, description) {
if (logLevel <= logLevels['debug']) {
console.log(prefix, name, description);
}
if (logDebugToService) {
logToService('debug', name, description);
}
}
function info(name, description) {
if (logLevel <= logLevels['info']) {
console.info(prefix, name, description);
}
}
function warn(name, description) {
if (logLevel <= logLevels['warn']) {
console.warn(prefix, name, description);
}
}
function error(name, description) {
if (logLevel <= logLevels['error']) {
console.error(prefix, name, description);
logToService('error', name, description);
}
}
/**
* subscribes to jwplayer errors
*
* @param playerInstance
*/
function subscribeToPlayerErrors(playerInstance) {
playerInstance.on('setupError', function (err) {
error('setupError', err);
});
playerInstance.on('error', function (err) {
error('error', err);
});
}
return {
debug: debug,
info: info,
warn: warn,
error: error,
subscribeToPlayerErrors: subscribeToPlayerErrors
};
}