Skip to content

Commit 504c86b

Browse files
alexk-blackopsalexk-blackops
authored andcommitted
minor fixes and comments
1 parent a5c7ca5 commit 504c86b

File tree

6 files changed

+38
-50
lines changed

6 files changed

+38
-50
lines changed

lib/api.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ module.exports.methods = {
4646
console.log('successfully identified');
4747

4848
CONFIG.APP_DETAILS = data;
49-
console.log(util.inspect(data));
50-
CONFIG.APP_DETAILS.ENV = settings.env;
5149
CONFIG.APIKEY = settings.apiKey;
5250

5351
logger.methods.init();
@@ -63,18 +61,19 @@ module.exports.methods = {
6361
};
6462

6563
console.log('Identifying...');
66-
64+
6765
// check that settings object is correct
6866
if (!settings) {
6967
throw new TypeError('Settings are not provided');
7068
}
7169

72-
if (typeof (settings.apiKey) === 'string' && typeof (settings.env) === 'string') {
70+
if (setting.apiKey && typeof (settings.apiKey) === 'string') {
7371
opt.headers['X-Stackify-Key'] = settings.apiKey;
7472
CONFIG.APPNAME = appname;
73+
CONFIG.ENV = settings.env;
7574
send(opt, callback, fail);
7675
} else {
77-
throw new TypeError('You have to pass API key and env parameters');
76+
throw new TypeError('You have to pass API key');
7877
}
7978

8079
},

lib/error.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var url = require('url'),
22
util = require('util'),
3+
os = require('os'),
34

45
stackTrace = require('stack-trace'),
56

@@ -46,7 +47,7 @@ var url = require('url'),
4647
/* remove methods of logger itself from the stack trace */
4748
trace.forEach(function (val, index, arr) {
4849
var method = val.methodName || (val.functionName || val.typeName) + '.<anonymous>';
49-
50+
5051
if (method.search('lib/logger.js') < 0 && method.search('stackify') < 0) {
5152
result.push({
5253
CodeFileName: val.fileName,
@@ -66,11 +67,11 @@ var url = require('url'),
6667
module.exports = {
6768
/* getting source method and source method line of code*/
6869
getStackTraceItem: function getStackTraceItem(err) {
69-
var trace = getTrace(err)
70+
var trace = getTrace(err);
7071
return {
7172
SrcMethod: trace[0].Method,
7273
SrcLine: trace[0].LineNum
73-
}
74+
};
7475
},
7576

7677
// create the exception details object
@@ -87,11 +88,11 @@ module.exports = {
8788
InnerError: null
8889
},
8990
EnvironmentDetail: {
90-
AppName: CONFIG.APP_DETAILS ? CONFIG.APP_DETAILS.AppName : CONFIG.APPNAME,
91-
AppNameID: CONFIG.APP_DETAILS ? CONFIG.APP_DETAILS.AppNameID : '',
92-
EnvID: CONFIG.APP_DETAILS ? CONFIG.APP_DETAILS.EnvID : '',
93-
AppEnvID: CONFIG.APP_DETAILS ? CONFIG.APP_DETAILS.AppEnvID : '',
94-
AppLocation: process.env.PWD
91+
DeviceName: os.hostname(),
92+
AppName: CONFIG.APPNAME,
93+
AppLocation: process.env.PWD,
94+
ConfiguredAppName: CONFIG.APPNAME,
95+
ConfiguredEnvironmentName: CONFIG.ENV
9596
},
9697
ServerVariables: process.env
9798
},

lib/exception.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ module.exports = {
6666
},
6767
// Express error handling middleware
6868
expressExc : function expressExc(err, req, res, next) {
69-
console.log('err');
7069

7170
var cb = function () {
7271
next(err);

lib/helpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module.exports.parseMeta = function parseMeta(meta, err) {
2121
return {
2222
result: result,
2323
ex: ex
24-
}
24+
};
2525
} catch (e) {
2626
throw new TypeError('Metadata should be valid JSON Object');
2727
}

lib/logger.js

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,15 @@ var api = require('./api'),
1717

1818
// automatically send all the messages from the queue after some timeout
1919
checkLogs = function () {
20-
var length = storage.length;
20+
var length = Math.min(storage.length, CONFIG.MSG_LIMIT);
2121
if (flag && length) {
2222
sendLogs(length);
2323
}
2424
console.log('logs checked');
2525
},
2626
// handler for sending logs
27-
sendLogs = function (len) {
27+
sendLogs = function (length) {
2828
var self = this,
29-
length = len || CONFIG.MSG_LIMIT,
3029
data = storage.slice(0, length),
3130

3231
/* if request is succesful remove messages from the queue, change the timeout for the next checkLogs call,
@@ -38,11 +37,14 @@ var api = require('./api'),
3837
storage = storage.slice(length);
3938
console.log('logs sent');
4039

40+
// if there are enough messages for the batch, send another request
41+
4142
if (storage.length >= CONFIG.MSG_LIMIT) {
4243
sendLogs();
4344
} else {
4445
flag = true;
4546

47+
// reset previous timeout, define the new one
4648
if (timeout) {
4749
clearTimeout(timeout);
4850
}
@@ -67,9 +69,10 @@ var api = require('./api'),
6769
}
6870
},
6971
/**
70-
Check for duplicated error messages
72+
Check for duplicate error messages. If the same error message logged more than configurated limit in one minute
73+
don't push it to the queue
7174
*/
72-
checkErrorLimitMessage = function checkErrorLimitMessage (ex) {
75+
checkErrorLimitMessage = function checkErrorLimitMessage(ex) {
7376
var min = new Date().getMinutes(),
7477
key = ex.Error.Message + ex.Error.ErrorType + ex.Error.SourceMethod;
7578

@@ -79,12 +82,12 @@ var api = require('./api'),
7982

8083
if (error_storage[min]) {
8184
if (error_storage[min][key]) {
82-
error_storage[min][key] += 1
85+
error_storage[min][key] += 1;
8386
} else {
8487
error_storage[min][key] = 1;
8588
}
8689
} else {
87-
errorStorage = {};
90+
error_storage = {};
8891
error_storage[min] = {};
8992
error_storage[min][key] = 1;
9093
}
@@ -108,6 +111,7 @@ module.exports.methods = {
108111
},
109112
data;
110113

114+
// handle properly metadata object and create exception object if needed
111115
if (meta) {
112116
if (level.toLowerCase() === 'error') {
113117
data = helpers.parseMeta(meta, true);
@@ -131,55 +135,44 @@ module.exports.methods = {
131135
storage.push(rec);
132136
console.log('logged');
133137

138+
// remove the earliest message from the queue if message cap is exceeded
134139
if (storage.length === CONFIG.MSG_CAP) {
135140
storage.shift();
136141
}
137142
}
138-
143+
139144
if (flag && storage.length >= CONFIG.MSG_LIMIT && CONFIG.APIKEY) {
140145
sendLogs();
141146
}
142147
},
143148

144-
/* check the queue after IdentifyApp call is done, switch the flag, set the timeout for checking the queue,
149+
/* check the queue after IdentifyApp call is done, switch the flag, set the timeout for the next checking the queue,
145150
set environment details for already logged messages
146151
*/
147152
init: function init() {
148-
console.log('flag switched');
149153
flag = true;
150154

151155
timeout = setTimeout(checkLogs, CONFIG.SCAN_TIMER);
152156

153-
storage.forEach(function(elem, index, arr) {
154-
if (elem.ex) {
155-
elem.ex.EnvironmentDetail = {
156-
AppName: CONFIG.APP_DETAILS ? CONFIG.APP_DETAILS.AppName : CONFIG.APPNAME,
157-
AppNameID: CONFIG.APP_DETAILS ? CONFIG.APP_DETAILS.AppNameID : '',
158-
EnvID: CONFIG.APP_DETAILS ? CONFIG.APP_DETAILS.EnvID : '',
159-
AppEnvID: CONFIG.APP_DETAILS ? CONFIG.APP_DETAILS.AppEnvID : '',
160-
AppLocation: process.env.PWD
161-
};
162-
}
163-
});
164-
165157
if (storage.length >= CONFIG.MSG_LIMIT) {
166158
sendLogs();
167159
}
168160
},
169-
161+
170162
log: function log(level, msg, meta) {
171163
var levels = ['error', 'debug', 'warn', 'info', 'trace'];
172164

165+
// check the message level
173166
if (levels.indexOf(level.toLowerCase()) < 0) {
174-
throw new TypeError(level + 'doesn\'t exist');
167+
throw new TypeError(level + ' level doesn\'t exist');
175168
}
176169

177170
this.push(level, msg, meta);
178171
},
179172

180-
/*
181-
Shortcut functions for logging message of certain level. Every function takes the same params as log function except the level.
182-
*/
173+
/*
174+
Shortcut functions for logging message of certain level. Every function takes the same params as log function except the level.
175+
*/
183176

184177
trace: function trace(msg, meta) {
185178
this.log('trace', msg, meta);

lib/sender.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
/* ### function sender (options, data, [cb], [fail])
2-
###### @options {Object} request options (hostname, headers, path, etc).
3-
###### @data {Object} data to be sent
2+
###### @options {Object} request options (hostname, headers, path, post data etc).
43
###### @cb {Function} **Optional** callback function to be executed if request was succesful
54
###### @fail {Function} **Optional** function to be executed if request wasn't succesful
65
Low level function for sending http/https requests
76
*/
87
var http = require('http'),
98
util = require('util'),
10-
9+
1110
request = require('request');
1211

1312
module.exports = function sender(options, cb, fail) {
14-
console.log('here');
15-
console.log(util.inspect(options));
13+
1614
var callback = function (error, response, body) {
1715
if (!error) {
1816
if (response.statusCode === 200) {
@@ -30,8 +28,6 @@ module.exports = function sender(options, cb, fail) {
3028
console.log(error);
3129
}
3230
};
33-
34-
request(options, callback);
35-
3631

32+
request(options, callback);
3733
};

0 commit comments

Comments
 (0)