Skip to content

Commit 1dac360

Browse files
alexk-blackopsalexk-blackops
authored andcommitted
different fixes and improvements
1 parent df69b5b commit 1dac360

File tree

9 files changed

+232
-77
lines changed

9 files changed

+232
-77
lines changed

config/config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ module.exports = {
55
'LOG_SAVE_PATH': '/API/Log/Save',
66
'LICENSE_KEY': '0Zw8Fj4Hr3Aa1Sf2Gw4Cb3Gk7Fp6Zn6Sc0Gw2Cr',
77
'MSG_LIMIT': 20, // number of messages
8-
'SCAN_TIMER': 30, // sec
9-
'REQUEST_TIMER': 30 * 1000, // sec
10-
'REQUEST_ATTEMPTS': 20, // number of attempts if API call isn't succesful
8+
'SCAN_TIMER': 30,
9+
'REQUEST_TIMER': 5 * 1000,
10+
'REQUEST_ATTEMPTS': 5, // number of attempts if API call isn't succesful
1111
'ERROR_FLOOD_LIMIT': 100, // limit of the same error message per minute
1212
'COOKIE_MASK': 'X-MASKED-X'
1313
};

index.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
1-
var api = require('./lib/api'),
2-
CONFIG = require('./config/config'),
3-
error = require('./lib/error'),
4-
exc = require('./lib/exception'),
5-
helpers = require('./lib/helpers'),
6-
logger = require('./lib/logger'),
7-
sender = require('./lib/sender');
1+
var api = require('./lib/api'), // wrappers for API calls
2+
CONFIG = require('./config/config'), // configuration settings file
3+
error = require('./lib/error'), // error parser module
4+
exc = require('./lib/exception'), // exception handler module
5+
helpers = require('./lib/helpers'), // different helpers functions
6+
logger = require('./lib/logger'), // logging methods module
7+
sender = require('./lib/sender'); // wrapper for http/https requests
88

99
module.exports = function (options) {
1010

1111
api.identifyApp(options);
12-
exc.exc();
13-
12+
/*exc.exc();
13+
*/
1414
return {
1515
storage: logger.storage,
1616
CONFIG: CONFIG,
1717

1818
log: logger.methods.log,
19+
trace: logger.methods.trace,
1920
debug: logger.methods.debug,
2021
info: logger.methods.info,
2122
warn: logger.methods.warn,
@@ -25,7 +26,6 @@ module.exports = function (options) {
2526

2627
checkError: error.checkError,
2728

28-
excCaught: exc.excCaught,
2929
excHandler: exc.exc,
3030
expressExcHandler: exc.expressExc
3131
};

lib/api.js

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
var os = require('os'),
2-
send = require('./sender'),
3-
logger = require('./logger'),
4-
CONFIG = require('../config/config'),
5-
pkginfo = require('pkginfo')(module, 'name'),
1+
var os = require('os'),
2+
util = require('util'),
3+
send = require('./sender'),
4+
logger = require('./logger'),
5+
CONFIG = require('../config/config'),
6+
pkginfo = require('pkginfo')(module, 'name', 'version'),
7+
8+
appname = module.exports.name,
69

710
options = {
811
hostname: CONFIG.HOST,
@@ -20,23 +23,31 @@ module.exports = {
2023

2124
identifyApp: function identifyApp(settings) {
2225
var opt = options,
26+
fail_counter = 0,
2327
data = {
24-
"DeviceName": os.hostname(),
25-
"AppName": module.exports.name
28+
DeviceName: os.hostname(),
29+
AppName: appname,
30+
ConfiguredAppName: appname,
31+
AppLocaton: process.env.PWD
2632
},
2733
callback = function (data) {
2834
CONFIG.APP_DETAILS = data.toJSON();
2935
CONFIG.LICENSE_KEY = settings.license_key;
3036
},
3137
fail = function () {
32-
setTimeout(function () {
33-
send(options, data, callback, fail);
34-
}, CONFIG.REQUEST_TIMER);
38+
fail_counter += 1;
39+
if (fail_counter <= CONFIG.REQUEST_ATTEMPTS) {
40+
setTimeout(function () {
41+
send(options, data, callback, fail);
42+
}, CONFIG.REQUEST_TIMER);
43+
}
3544
};
36-
opt.path = CONFIG.IDENTIFY_PATH;
37-
45+
46+
opt.path = CONFIG.IDENTIFY_PATH;
47+
3848
if (typeof (settings.license_key) === 'string') {
3949
opt.headers['X-Stackify-Key'] = settings.license_key;
50+
CONFIG.APPNAME = appname;
4051
send(options, data, callback, fail);
4152
} else {
4253
throw new TypeError('License key is not defined or has a wrong format');
@@ -46,9 +57,22 @@ module.exports = {
4657

4758
postLogs: function postLogs(messages, cb, fail) {
4859
var opt = options,
49-
data = CONFIG.APP_DETAILS;
60+
data = {
61+
CDID: CONFIG.APP_DETAILS.CDID,
62+
CDAppID: CONFIG.APP_DETAILS.CDAppID,
63+
AppNameID: CONFIG.APP_DETAILS.AppNameID,
64+
AppEnvID: CONFIG.APP_DETAILS.AppEnvID,
65+
EnvID: CONFIG.APP_DETAILS.EnvID,
66+
Env: CONFIG.APP_DETAILS.Env,
67+
ServerName: os.hostname(),
68+
AppName: appname,
69+
AppLoc: process.env.PWD,
70+
Logger: appname + app,
71+
Platform: 'Node.js',
72+
Msgs : messages
73+
};
74+
5075

51-
data.Msgs = messages;
5276
opt.path = CONFIG.LOG_SAVE_PATH;
5377

5478
send(options, data, cb, fail);

lib/error.js

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
var url = require('url'),
2+
util = require('util'),
23
stackTrace = require('stack-trace'),
34
helpers = require('./helpers'),
45
CONFIG = require('../config/config'),
5-
6+
67
getCookies = function getCookies(req) {
78
var key,
89
keys,
@@ -33,35 +34,68 @@ var url = require('url'),
3334
result = req.body;
3435
}
3536
return result;
37+
},
38+
getTrace = function getTrace(err) {
39+
var trace = stackTrace.parse(err),
40+
result = [];
41+
42+
trace.forEach(function (val, index, arr) {
43+
result.push({
44+
CodeFileName: val.fileName,
45+
LineNum: val.lineNumber,
46+
Method: val.methodName || (val.functionName || val.typeName) + '.<anonymous>'
47+
});
48+
});
49+
return result;
50+
},
51+
checkTrace = function CheckTrace(trace) {
52+
if (trace[1].CodeFileName.search('lib/logger.js') >= 0) {
53+
trace.splice(1, 1);
54+
}
55+
return trace;
56+
},
57+
getURL = function getURL(req) {
58+
var href = (req.connection.encrypted ? "https://" : "http://") + req.headers.host + req.url;
59+
return href;
3660
};
3761

3862
module.exports = {
39-
formatEx : function formatEx(err, req) {
40-
var trace = this.parseError(err),
63+
getStackTraceItem: function getStackTraceItem(err) {
64+
var trace = getTrace(err);
65+
trace = checkTrace(trace);
66+
return {
67+
SrcMethod: trace[1].Method,
68+
SrcLine: trace[1].LineNum
69+
}
70+
},
71+
formatEx : function formatEx(err, req, msg) {
72+
var trace = getTrace(err),
73+
newTrace = checkTrace(trace),
4174
ex = {
4275
OccuredEpochMillis: Date.now(),
4376
Error: {
44-
Message: err.method,
45-
ErrorType: err.name,
46-
SourceMethod: trace[0],
47-
StackTrace: trace,
77+
Message: msg || err.message,
78+
ErrorType: err.name || 'StringException',
79+
ErrorTypeCode: null,
80+
SourceMethod: msg ? newTrace[1].Method : trace[1].Method,
81+
StackTrace: msg ? newTrace : trace,
4882
InnerError: null
4983
},
5084
EnvironmentDetail: {
51-
AppName: CONFIG.APP_DETAILS.AppName,
52-
AppNameID: CONFIG.APP_DETAILS.AppNameID,
53-
EnvID: CONFIG.APP_DETAILS.EnvID,
54-
AppEnvID: CONFIG.APP_DETAILS.AppEnvID,
55-
AppLocation: process.env.PWD,
85+
AppName: CONFIG.APP_DETAILS ? CONFIG.APP_DETAILS.AppName : CONFIG.APPNAME,
86+
AppNameID: CONFIG.APP_DETAILS ? CONFIG.APP_DETAILS.AppNameID : '',
87+
EnvID: CONFIG.APP_DETAILS ? CONFIG.APP_DETAILS.EnvID : '',
88+
AppEnvID: CONFIG.APP_DETAILS ? CONFIG.APP_DETAILS.AppEnvID : '',
89+
AppLocation: process.env.PWD
5690
},
5791
ServerVariables: process.env
5892
},
59-
headers = req.headers,
60-
qs,
93+
headers = req ? req.headers : {},
94+
href,
6195
key;
6296

6397
if (req) {
64-
qs = url.parse(req.url, true);
98+
href = url.parse(getURL(req), true);
6599

66100
if (req.headers.cookie) {
67101
headers = {};
@@ -75,14 +109,14 @@ module.exports = {
75109
ex.WebRequestDetail = {
76110
UserIPAddress : req.headers['x-forwarded-for'] || req.connection.remoteAddress || req.socket.remoteAddress ||
77111
req.connection.socket.remoteAddress,
78-
HttpMethod: req.protocol,
79-
RequestProtocol: req.method,
80-
RequestUrl: req.url,
81-
RequestUrlRoot: qs.pathname,
112+
HttpMethod: req.method,
113+
RequestProtocol: req.connection.encrypted ? 'HTTPS' : 'HTTP',
114+
RequestUrl: href.hostname + href.pathname,
115+
RequestUrlRoot: href.hostname,
82116
ReferralUrl: req.headers.referer,
83117
Headers: headers,
84118
Cookies: getCookies(req),
85-
QueryString: qs.query,
119+
QueryString: href.query,
86120
PostData: getPostData(req)
87121
};
88122
}

lib/exception.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
var qs = require('querystring'),
2+
util = require('util'),
23
error = require('./error'),
34
logger = require('./logger'),
45
api = require('./api'),
56
CONFIG = require('../config/config'),
67
handler = function handler(err, req, cb) {
78
var rec = {
8-
Msg: err.method + ': ' + err.name,
9+
Msg: err.name + ': ' + err.message,
910
Level: 'ERROR',
10-
EpochMs: new Date().toUTCString(),
11+
EpochMs: Date.now(),
1112
Ex: error.formatEx(err, req)
1213
},
1314
fail_counter = 0,
@@ -20,35 +21,42 @@ var qs = require('querystring'),
2021
}
2122
};
2223

23-
if (CONFIG.APP_DETAILS) {
24+
logger.storage.push(rec);
25+
console.log(util.inspect(rec, {depth: null}));
26+
27+
/* if (CONFIG.APP_DETAILS) {
2428
logger.storage.push(rec);
2529
api.postLogs(logger.storage, cb, fail);
26-
}
30+
}*/
2731
},
2832
excCaught = false;
2933

3034
module.exports = {
31-
excCaught: excCaught,
32-
exc : function exc(req) {
33-
return function() {
35+
exc : function exc(req, res) {
36+
return (function () {
3437
var body = '';
3538
if (req) {
3639
req.on('data', function (chunk) {
40+
console.log('here');
3741
body += chunk;
3842
});
3943
req.on('end', function () {
4044
var json = qs.parse(body);
4145
req.body = json;
46+
console.log('body: ', body);
4247
});
4348
}
4449

4550
process.on('uncaughtException', function (err) {
4651
if (!excCaught) {
4752
excCaught = true;
48-
handler(err, req);
53+
handler(err, req);
54+
if (res) {
55+
res.end(JSON.stringify(logger.storage));
56+
}
4957
}
5058
});
51-
};
59+
}());
5260
},
5361

5462
expressExc : function expressExc(err, req, res, next) {

lib/helpers.js

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
1-
module.exports.checkMeta = function checkMeta(meta) {
1+
/* ### function parseMeta (meta, [err])
2+
###### @meta {Object} any meta data added to the message.
3+
###### @err {Boolean} **Optional** if true we should look for an error object and exclude it to separate parameter
4+
Function for parsing meta objects attached to the message
5+
*/
6+
7+
module.exports.parseMeta = function parseMeta(meta, err) {
8+
var result,
9+
ex,
10+
key;
211
if (Object.prototype.toString.call(meta) === '[object Object]') {
3-
if (Object.keys(meta).length === 1) {
4-
try {
5-
JSON.stringify(meta);
6-
} catch (e) {
7-
return false;
12+
for (key in meta) {
13+
if (meta[key] instanceof Error && err && !ex) {
14+
ex = meta[key];
15+
delete meta[key];
816
}
9-
return [Object.keys(meta)[0], meta[Object.keys(meta)[0]]];
1017
}
18+
try {
19+
result = JSON.stringify(meta);
20+
21+
return {
22+
result: result,
23+
ex: ex
24+
}
25+
} catch (e) {
26+
throw new TypeError('Metadata should be valid JSON Object');
27+
}
28+
} else {
29+
throw new TypeError('Metadata should be valid JSON Object');
1130
}
12-
return false;
31+
1332
};

0 commit comments

Comments
 (0)