-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathangular-splunk-logger.js
434 lines (343 loc) · 11.4 KB
/
angular-splunk-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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/**
* splunkLogger is a module which will send your log messages to a configured
* [Splunk](http://splunk.com) server.
*
* This is based on https://github.com/ajbrown/angular-loggly-logger by ajbrown
*/
; (function( angular ) {
"use strict";
angular.module( 'splunkLogger.logger', [] )
.provider( 'SplunkLogger', function() {
var self = this;
var _logLevels = [ 'DEBUG', 'INFO', 'WARN', 'ERROR' ];
var _fields = {};
var _includeCurrentUrl = false;
var _includeTimestamp = false;
var _includeUserAgent = false;
var _tag = null;
var _sendConsoleErrors = false;
var _logToConsole = true;
var _loggingEnabled = true;
var _labels = {};
var _source = null;
var _errorThreshold = null;
// The minimum level of messages that should be sent to splunk.
var _level = 0;
var _token = null;
var _endpoint = null;
var _logToSplunk = logToSplunkNoErrorChecking;
// configuration methods for this provider
this.endpoint = endpoint;
this.token = token;
this.source = source;
this.errorThreshold = errorThreshold;
this.fields = fields;
this.labels = labels;
this.includeUrl = includeUrl;
this.includeTimestamp = includeTimestamp;
this.includeUserAgent = includeUserAgent;
this.inputTag = inputTag;
this.sendConsoleErrors = sendConsoleErrors;
this.level = level;
this.loggingEnabled = loggingEnabled;
this.isLevelEnabled = isLevelEnabled;
this.logToConsole = logToConsole;
function endpoint(e) {
if (angular.isDefined(e)) {
_endpoint = e;
return self;
}
return _endpoint;
}
function token(t) {
if (angular.isDefined(t)) {
_token = t;
return self;
}
return _token;
}
function source(s) {
if (angular.isDefined(s)) {
_source = s;
return self;
}
return _source;
}
function fields(d) {
if (angular.isDefined(d) ) {
_fields = d;
return self;
}
return _fields;
}
function labels(l) {
if (angular.isObject(l)) {
_labels = l;
return self;
}
return _labels;
}
function includeUrl(flag) {
if (angular.isDefined(flag)) {
_includeCurrentUrl = !!flag;
return self;
}
return _includeCurrentUrl;
}
function includeTimestamp(flag) {
if (angular.isDefined(flag)) {
_includeTimestamp = !!flag;
return self;
}
return _includeTimestamp;
}
function includeUserAgent(flag) {
if (angular.isDefined(flag)) {
_includeUserAgent = !!flag;
return self;
}
return _includeUserAgent;
}
function inputTag(usrTag){
if (angular.isDefined(usrTag)) {
_tag = usrTag;
return self;
}
return _tag;
}
function sendConsoleErrors(flag){
if (angular.isDefined(flag)) {
_sendConsoleErrors = !!flag;
return self;
}
return _sendConsoleErrors;
}
function level(name) {
if (angular.isDefined(name)) {
var newLevel = _logLevels.indexOf(name.toUpperCase());
if (newLevel < 0) {
throw "Invalid logging level specified: " + name;
} else {
_level = newLevel;
}
return self;
}
return _logLevels[_level];
}
function isLevelEnabled(name) {
return _logLevels.indexOf(name.toUpperCase()) >= _level;
}
function loggingEnabled(flag) {
if (angular.isDefined(flag)) {
_loggingEnabled = !!flag;
return self;
}
return _loggingEnabled;
}
function logToConsole(flag) {
if (angular.isDefined(flag)) {
_logToConsole = !!flag;
return self;
}
return _logToConsole;
}
// sets the error threshold and setups the correct logging function
function errorThreshold(e) {
if (angular.isDefined(e)) {
_errorThreshold = e;
if (_errorThreshold === null ) {
_logToSplunk = logToSplunkNoErrorChecking;
}
else {
_logToSplunk = getLogToSplunkWithErrorCheckingFunction();
}
return self;
}
return _errorThreshold;
}
// if no error threshold, use this function to blindly log
function logToSplunkNoErrorChecking($http, _endpoint, splunkEvent, config) {
$http.post(_endpoint, splunkEvent, config);
}
// if error threshold, check before logging and track success and failure
function getLogToSplunkWithErrorCheckingFunction() {
var errorCount = 0;
return function($http, _endpoint, splunkEvent, config) {
if (errorCount < _errorThreshold) {
$http.post(_endpoint, splunkEvent, config).then(
function() {
errorCount = 0;
},
function() {
++errorCount;
}
);
}
};
}
//
// SplunkLogger object to be used in application
//
this.$get = [ '$injector', function ($injector) {
var lastLog = null;
/**
* Send the specified data to splunk as a json message.
* @param data
*/
var sendMessage = function(data) {
//If a token is not configured, don't do anything.
if (!_token || !_endpoint || !_loggingEnabled) {
return;
}
// Create the event based on configuration
function createSplunkEvent(data) {
//TODO we're injecting this here to resolve circular dependency issues. Is this safe?
var $window = $injector.get('$window');
var $location = $injector.get('$location');
var splunkEvent = {};
// these are native splunk event fields
if (_source) {
splunkEvent.source = _source;
}
if (_includeTimestamp) {
splunkEvent.time = (lastLog.getTime() / 1000);
}
// this is our custom event object
var eventData = angular.extend({}, _fields, data);
if (_includeCurrentUrl) {
eventData.url = $location.absUrl();
}
if (_includeUserAgent) {
eventData.userAgent = $window.navigator.userAgent;
}
// Apply labels overrides if the exist
for (var label in _labels) {
if (label in eventData) {
eventData[_labels[label]] = eventData[label];
delete eventData[label];
}
}
splunkEvent.event = eventData;
return splunkEvent;
}
//we're injecting $http
var $http = $injector.get('$http');
lastLog = new Date();
var splunkEvent = createSplunkEvent(data);
//Set header for splunk
var config = {
headers: {
'Authorization': 'Splunk ' + _token,
'Cache-Control': undefined,
'Content-Type': undefined
},
responseType: 'json',
withCredentials: true
};
_logToSplunk($http, _endpoint, splunkEvent, config);
};
return {
lastLog: function() { return lastLog; },
sendConsoleErrors: function() { return _sendConsoleErrors; },
level: function() { return _level; },
loggingEnabled: loggingEnabled,
isLevelEnabled : isLevelEnabled,
sendMessage: sendMessage,
logToConsole: logToConsole,
token: token,
endpoint: endpoint,
fields: fields
};
}];
} )
;
angular.module( 'splunkLogger', ['splunkLogger.logger'] )
.config( [ '$provide', function($provide) {
$provide.decorator('$log', [ "$delegate", '$injector', function($delegate, $injector) {
var logger = $injector.get('SplunkLogger');
// install a window error handler
if (logger.sendConsoleErrors() === true) {
var _onerror = window.onerror;
//send console error messages to Splunk
window.onerror = function (msg, url, line, col, error) {
logger.sendMessage({
level : 'ERROR',
message: msg,
line: line,
col: col,
stack: error && error.stack
});
if (_onerror && typeof _onerror === 'function') {
_onerror.apply(window, arguments);
}
};
}
var wrapLogFunction = function(logFn, level, loggerName) {
var wrappedFn = function () {
var args = Array.prototype.slice.call(arguments);
if (logger.logToConsole()) {
logFn.apply(null, args);
}
// Skip messages that have a level that's lower than the configured level for this logger.
if (!logger.loggingEnabled() || !logger.isLevelEnabled(level)) {
return;
}
var msg = (args.length === 1 ? args[0] : args) || {};
var sending = { level: level };
if (angular.isDefined(msg.stack) || (angular.isDefined(msg[0]) && angular.isDefined(msg[0].stack))) {
//handling console errors
if (logger.sendConsoleErrors() === true) {
sending.message = msg.message || msg[0].message;
sending.stack = msg.stack || msg[0].stack;
}
else {
return;
}
}
else if (angular.isObject(msg)) {
//handling JSON objects
sending = angular.extend({}, msg, sending);
}
else {
//sending plain text
sending.message = msg;
}
if (loggerName) {
sending.logger = msg;
}
//Send the message to through the splunk sender
logger.sendMessage(sending);
};
wrappedFn.logs = [];
return wrappedFn;
};
var _$log = (function ($delegate) {
return {
log: $delegate.log,
info: $delegate.info,
warn: $delegate.warn,
error: $delegate.error
};
})($delegate);
var getLogger = function(name) {
return {
log: wrapLogFunction(_$log.log, 'INFO', name),
debug: wrapLogFunction(_$log.debug, 'DEBUG', name),
info: wrapLogFunction(_$log.info, 'INFO', name),
warn: wrapLogFunction(_$log.warn, 'WARN', name),
error: wrapLogFunction(_$log.error, 'ERROR', name)
};
};
//wrap the existing API
$delegate.log = wrapLogFunction($delegate.log, 'INFO');
$delegate.debug = wrapLogFunction($delegate.debug, 'DEBUG');
$delegate.info = wrapLogFunction($delegate.info, 'INFO');
$delegate.warn = wrapLogFunction($delegate.warn, 'WARN');
$delegate.error = wrapLogFunction($delegate.error, 'ERROR');
//Add some methods
$delegate.getLogger = getLogger;
return $delegate;
}]);
}])
;
})(window.angular);