-
Notifications
You must be signed in to change notification settings - Fork 0
/
angular-elasticsearch-logger.js
197 lines (166 loc) · 7.36 KB
/
angular-elasticsearch-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
/**
* @ngdoc function
* @name esLogging.esLogProvider
* @description
* # MainCtrl
* Controller of the clientSideApp
*/
angular.module('cmanaha.angular-elasticsearch-logger',['elasticsearch'])
.provider('CMRESLogger', [function ()
{
var self = this;
self.esClientDetails = {};
self.logDetails = {
'index': 'defaul_angularjs_index',
'type': 'jslog',
'bufferSize': 5000,
'flushIntervalInMS': 2500,
};
self.appDetails = {};
self.setElasticSearchConfig = function (elasticSearchConfig) {
self.esClientDetails = elasticSearchConfig;
};
self.getElasticSearchConfig = function(){
var res = angular.copy(self.esClientDetails);
return res;
};
self.setLogConfig = function (applicationLogConfig) {
self.logDetails = applicationLogConfig;
};
self.getLogConfig = function(){
var res = angular.copy(self.logDetails);
return res;
};
self.setApplicationLogContext = function (applicationLogConfig) {
self.appDetails = applicationLogConfig;
};
self.getApplicationLogContext = function(){
var res = angular.copy(self.appDetails);
return res;
};
//self.$get = ['$interval', 'esFactory', function ($interval, esFactory) {
self.$get = ['$injector', function ($injector) {
var esFactory = $injector.get('esFactory');
var interval = $injector.get('$interval');
self.logBuffer = [];
var esFact = esFactory;
self.esClient = esFact(self.esClientDetails);
self.level = {
DEBUG: 'DEBUG',
INFO: 'INFO',
WARN: 'WARNING',
ERROR: 'ERROR'
};
self.numPadding = function (num, size) {
var ret = num + '';
while (ret.length < size) {
ret = '0' + ret;
}
return ret;
};
self.getIndexName = function (indexPrefx) {
var d = new Date(Date.now());
var ret = indexPrefx + '-' + d.getUTCFullYear() + '.' + self.numPadding(d.getUTCMonth() + 1, 2) + '.' + self.numPadding(d.getUTCDate(), 2);
return ret;
};
self.decodeStackTraceLine = function (line) {
var lineTrc = line.split('.js');
var method = lineTrc[0] + '.js';
var lineNum = lineTrc[1].split(':')[1];
var charNum = lineTrc[1].split(':')[2];
return [method, lineNum, charNum];
};
self.getVariableWithDefault = function (variable, defaultValue){
var res = 'Unknown';
if (typeof variable === 'undefined'){
res = defaultValue;
} else {
res = variable;
}
return res;
};
self.internalLog = function (logMsg, level, exception, properties) {
//Add operation to the backlog
self.logBuffer.push(
{
index: {
_index: self.getIndexName(self.logDetails.index),
_type: self.logDetails.type,
}
}
);
var data = {
message: logMsg,
level: level,
timestamp: new Date(Date.now()).toISOString(),
browserCodeName: self.getVariableWithDefault(navigator.appCodeName,'Unknown'),
browserName: self.getVariableWithDefault(navigator.appName,'Unknown'),
browserVersion: self.getVariableWithDefault(navigator.appVersion,'Unknown'),
browserProduct: self.getVariableWithDefault(navigator.product,'Unknown'),
browserPlatform: self.getVariableWithDefault(navigator.platform,'Unknown'),
browserUserAgent: self.getVariableWithDefault(navigator.userAgent,'Unknown'),
browserGeolocation: self.getVariableWithDefault(navigator.geolocation,'Unknown')
};
//add additional application data context
angular.forEach(self.appDetails, function (value, key) {
data[key] = value;
});
//add additional logging properties context
if (typeof (properties) !== 'undefined') {
angular.forEach( properties, function (value, key) {
data[key] = value;
});
}
//add details about the method, line number, etc.
//if the log was thrown from an exception, log the exception details and
//exception stacktrace
var trace = [];
var traceInfo = [];
if (typeof (exception) === 'undefined') {
trace = printStackTrace();
} else {
trace = printStackTrace(exception);
var relevantStacktrace = trace.slice(5);
var fullTraceStr = 'Exception Msg [ ' + exception.message + '] \n';
angular.forEach(relevantStacktrace, function (traceLine) {
traceInfo = self.decodeStackTraceLine(traceLine);
fullTraceStr = fullTraceStr + '\t' + traceInfo[0] + ' LineNum [' + traceInfo[1] + '] charNum [' + traceInfo[2] + ']\n';
});
data.stacktrace = fullTraceStr;
}
traceInfo = self.decodeStackTraceLine(trace[5]);
data.method = traceInfo[0];
data.lineNum = traceInfo[1];
data.charNum = traceInfo[2];
//Add document to the backlog
self.logBuffer.push(data);
if (self.logBuffer.length / 2 >= self.logDetails.bufferSize) {
self.internalFlush();
}
};
self.internalFlush = function () {
if (self.logBuffer.length > 0) {
self.esClient.bulk({
body: self.logBuffer
}, function (err, rsp) {
if (err) {
if (self.logDetails.internalLogFunction !== undefined){
self.logDetails.internalLogFunction('Got an error [' + err + ']while storing log : ' + angular.toJson(rsp, true));
}
}
});
self.logBuffer = [];
}
};
self.flushInterval = interval(self.internalFlush, self.logDetails.flushIntervalInMS);
return {
info: function (message, properties) { self.internalLog(message, self.level.INFO, undefined, properties); },
warning: function (message, properties) { self.internalLog(message, self.level.WARN, undefined, properties); },
error: function (message, properties) { self.internalLog(message, self.level.ERROR, undefined, properties); },
debug: function (message, properties) { self.internalLog(message, self.level.DEBUG, undefined, properties); },
warningWithException: function (message, exception, properties) { self.internalLog(message, self.level.WARN, exception, undefined); },
errorWithException: function (message, exception, properties) { self.internalLog(message, self.level.ERROR, exception, undefined); },
flush: function(){self.internalFlush();},
};
}];
}]);