Skip to content

Commit a22f0bd

Browse files
alexk-blackopsalexk-blackops
authored andcommitted
refactoring of code
1 parent b4207ec commit a22f0bd

File tree

9 files changed

+165
-130
lines changed

9 files changed

+165
-130
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
node_modules
1+
node_modules
2+
npm-debug.log

config/config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ module.exports = {
33
'PORT': 443,
44
'IDENTIFY_PATH': '/API/Metrics/IdentifyApp',
55
'LOG_SAVE_PATH': '/API/Log/Save',
6-
'LICENSE_KEY': '0Zw8Fj4Hr3Aa1Sf2Gw4Cb3Gk7Fp6Zn6Sc0Gw2Cr',
6+
'APIKEY': '0Zw8Fj4Hr3Aa1Sf2Gw4Cb3Gk7Fp6Zn6Sc0Gw2Cr',
77
'MSG_LIMIT': 20, // number of messages
8-
'SCAN_TIMER': 30,
8+
'SCAN_TIMER': 30 * 1000,
99
'REQUEST_TIMER': 5 * 1000,
1010
'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

index.js

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,22 @@ var api = require('./lib/api'), // wrappers for API calls
66
logger = require('./lib/logger'), // logging methods module
77
sender = require('./lib/sender'); // wrapper for http/https requests
88

9-
module.exports = function (options) {
9+
module.exports = {
1010

11-
api.identifyApp(options);
12-
/*exc.exc();
13-
*/
14-
return {
15-
storage: logger.storage,
16-
CONFIG: CONFIG,
11+
start: function(options) {
12+
api.methods.identifyApp(options);
13+
exc.exc();
14+
},
1715

18-
log: logger.methods.log,
19-
trace: logger.methods.trace,
20-
debug: logger.methods.debug,
21-
info: logger.methods.info,
22-
warn: logger.methods.warn,
23-
error: logger.methods.error,
16+
log: logger.methods.log,
17+
trace: logger.methods.trace,
18+
debug: logger.methods.debug,
19+
info: logger.methods.info,
20+
warn: logger.methods.warn,
21+
error: logger.methods.error,
2422

25-
postLogs: api.postLogs,
23+
push: logger.methods.push,
2624

27-
checkError: error.checkError,
28-
29-
excHandler: exc.exc,
30-
expressExcHandler: exc.expressExc
31-
};
25+
excHandler: exc.exc,
26+
expressExcHandler: exc.expressExc
3227
};

lib/api.js

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ var os = require('os'),
33
send = require('./sender'),
44
logger = require('./logger'),
55
CONFIG = require('../config/config'),
6-
pkginfo = require('pkginfo')(module, 'name', 'version'),
6+
pkginfo = require('pkginfo')(module, 'name'),
77

8-
appname = module.exports.name,
8+
appname = module.exports.name,
99

1010
options = {
1111
hostname: CONFIG.HOST,
@@ -14,12 +14,12 @@ var os = require('os'),
1414
secureProtocol: 'SSLv3_method',
1515
headers: {
1616
'Content-Type': 'application/json',
17-
'X-Stackify-Key': CONFIG.LICENSE_KEY,
17+
'X-Stackify-Key': CONFIG.APIKEY,
1818
'X-Stackify-PV': 'V1'
1919
}
2020
};
2121

22-
module.exports = {
22+
module.exports.methods = {
2323

2424
identifyApp: function identifyApp(settings) {
2525
var opt = options,
@@ -31,50 +31,56 @@ module.exports = {
3131
AppLocaton: process.env.PWD
3232
},
3333
callback = function (data) {
34-
CONFIG.APP_DETAILS = data.toJSON();
35-
CONFIG.LICENSE_KEY = settings.license_key;
34+
console.log('successfully identified');
35+
36+
CONFIG.APP_DETAILS = JSON.parse(data);
37+
CONFIG.APP_DETAILS.ENV = settings.env;
38+
CONFIG.APIKEY = settings.apiKey;
39+
40+
logger.methods.init();
41+
3642
},
3743
fail = function () {
3844
fail_counter += 1;
3945
if (fail_counter <= CONFIG.REQUEST_ATTEMPTS) {
4046
setTimeout(function () {
41-
send(options, data, callback, fail);
47+
send(opt, data, callback, fail);
4248
}, CONFIG.REQUEST_TIMER);
4349
}
4450
};
4551

4652
opt.path = CONFIG.IDENTIFY_PATH;
4753

48-
if (typeof (settings.license_key) === 'string') {
49-
opt.headers['X-Stackify-Key'] = settings.license_key;
54+
console.log('Identifying...');
55+
56+
if (typeof (settings.apiKey) === 'string' && typeof (settings.env) === 'string') {
57+
opt.headers['X-Stackify-Key'] = settings.apiKey;
5058
CONFIG.APPNAME = appname;
51-
send(options, data, callback, fail);
59+
send(opt, data, callback, fail);
5260
} else {
53-
throw new TypeError('License key is not defined or has a wrong format');
61+
throw new TypeError('You have to pass apiKey and env parameters');
5462
}
5563

5664
},
5765

5866
postLogs: function postLogs(messages, cb, fail) {
5967
var opt = options,
6068
data = {
61-
CDID: CONFIG.APP_DETAILS.CDID,
62-
CDAppID: CONFIG.APP_DETAILS.CDAppID,
69+
CDID: CONFIG.APP_DETAILS.DeviceID,
70+
CDAppID: CONFIG.APP_DETAILS.DeviceAppID,
6371
AppNameID: CONFIG.APP_DETAILS.AppNameID,
6472
AppEnvID: CONFIG.APP_DETAILS.AppEnvID,
6573
EnvID: CONFIG.APP_DETAILS.EnvID,
6674
Env: CONFIG.APP_DETAILS.Env,
6775
ServerName: os.hostname(),
6876
AppName: appname,
6977
AppLoc: process.env.PWD,
70-
Logger: appname + app,
78+
Logger: 'Node.js Stackify Logger ver. 1.0',
7179
Platform: 'Node.js',
7280
Msgs : messages
7381
};
7482

75-
7683
opt.path = CONFIG.LOG_SAVE_PATH;
77-
7884
send(options, data, cb, fail);
7985
}
8086
};

lib/error.js

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var url = require('url'),
33
stackTrace = require('stack-trace'),
44
helpers = require('./helpers'),
55
CONFIG = require('../config/config'),
6-
6+
77
getCookies = function getCookies(req) {
88
var key,
99
keys,
@@ -38,47 +38,44 @@ var url = require('url'),
3838
getTrace = function getTrace(err) {
3939
var trace = stackTrace.parse(err),
4040
result = [];
41-
41+
/* remove methods of logger itself from the stack trace */
4242
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-
});
43+
var method = val.methodName || (val.functionName || val.typeName) + '.<anonymous>';
44+
45+
if (method.search('lib/logger.js') < 0 && method.search('stackify') < 0) {
46+
result.push({
47+
CodeFileName: val.fileName,
48+
LineNum: val.lineNumber,
49+
Method: method
50+
});
51+
}
4852
});
4953
return result;
5054
},
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-
},
5755
getURL = function getURL(req) {
5856
var href = (req.connection.encrypted ? "https://" : "http://") + req.headers.host + req.url;
5957
return href;
6058
};
6159

6260
module.exports = {
61+
/* getting source method and source method line of code*/
6362
getStackTraceItem: function getStackTraceItem(err) {
64-
var trace = getTrace(err);
65-
trace = checkTrace(trace);
63+
var trace = getTrace(err)
6664
return {
67-
SrcMethod: trace[1].Method,
68-
SrcLine: trace[1].LineNum
65+
SrcMethod: trace[0].Method,
66+
SrcLine: trace[0].LineNum
6967
}
7068
},
7169
formatEx : function formatEx(err, req, msg) {
7270
var trace = getTrace(err),
73-
newTrace = checkTrace(trace),
7471
ex = {
7572
OccuredEpochMillis: Date.now(),
7673
Error: {
7774
Message: msg || err.message,
78-
ErrorType: err.name || 'StringException',
75+
ErrorType: msg ? 'StringException' : err.name,
7976
ErrorTypeCode: null,
80-
SourceMethod: msg ? newTrace[1].Method : trace[1].Method,
81-
StackTrace: msg ? newTrace : trace,
77+
SourceMethod: trace[0].Method,
78+
StackTrace: trace,
8279
InnerError: null
8380
},
8481
EnvironmentDetail: {
@@ -111,7 +108,7 @@ module.exports = {
111108
req.connection.socket.remoteAddress,
112109
HttpMethod: req.method,
113110
RequestProtocol: req.connection.encrypted ? 'HTTPS' : 'HTTP',
114-
RequestUrl: href.hostname + href.pathname,
111+
RequestUrl: (req.connection.encrypted ? 'https://' : 'http://') + href.hostname + href.pathname,
115112
RequestUrlRoot: href.hostname,
116113
ReferralUrl: req.headers.referer,
117114
Headers: headers,
@@ -122,19 +119,5 @@ module.exports = {
122119
}
123120

124121
return ex;
125-
},
126-
127-
checkError : function checkError(obj) {
128-
var meta = obj.meta,
129-
metaIsValid = helpers.checkMeta(meta),
130-
err;
131-
132-
if (metaIsValid && metaIsValid[1] instanceof Error) {
133-
err = metaIsValid[1];
134-
obj.Ex = this.formatEx(err);
135-
delete obj.meta;
136-
}
137-
138-
return obj;
139122
}
140123
};

lib/exception.js

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@ var qs = require('querystring'),
1616
fail_counter += 1;
1717
if (fail_counter < CONFIG.REQUEST_ATTEMPTS) {
1818
setTimeout(function () {
19-
api.postLogs(logger.storage, null, fail);
19+
api.methods.postLogs(logger.storage, null, fail);
2020
}, CONFIG.REQUEST_TIMER);
2121
}
2222
};
2323

24-
logger.storage.push(rec);
25-
console.log(util.inspect(rec, {depth: null}));
26-
27-
/* if (CONFIG.APP_DETAILS) {
24+
if (CONFIG.APP_DETAILS) {
25+
console.log('handled');
2826
logger.storage.push(rec);
29-
api.postLogs(logger.storage, cb, fail);
30-
}*/
27+
api.methods.postLogs(logger.storage, cb, fail);
28+
} else {
29+
console.log('not identified yet');
30+
}
3131
},
3232
excCaught = false;
3333

@@ -37,6 +37,7 @@ module.exports = {
3737
var body = '';
3838
if (req) {
3939
req.on('data', function (chunk) {
40+
console.log('here');
4041
body += chunk;
4142
});
4243
req.on('end', function () {
@@ -46,18 +47,18 @@ module.exports = {
4647
}
4748

4849
process.on('uncaughtException', function (err) {
50+
console.log('exc');
4951
if (!excCaught) {
5052
excCaught = true;
5153
handler(err, req);
52-
if (res) {
53-
res.end(JSON.stringify(logger.storage));
54-
}
5554
}
5655
});
5756
}());
5857
},
5958

6059
expressExc : function expressExc(err, req, res, next) {
60+
console.log('err');
61+
6162
var cb = function () {
6263
next(err);
6364
};
@@ -66,11 +67,6 @@ module.exports = {
6667
return next();
6768
}
6869

69-
if (!excCaught) {
70-
excCaught = true;
71-
handler(err, req, cb);
72-
}
73-
74-
next(err);
70+
handler(err, req, cb);
7571
}
7672
};

0 commit comments

Comments
 (0)