Skip to content

Commit 9845d2a

Browse files
alexk-blackopsalexk-blackops
authored andcommitted
different minor style fixes
1 parent ab3927a commit 9845d2a

File tree

7 files changed

+75
-54
lines changed

7 files changed

+75
-54
lines changed

.gitignore

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
11
node_modules
2-
npm-debug.log
3-
proxy.js
4-
proxy2.js
5-
new.js
2+
npm-debug.log

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ The following options could be passed. 'apiKey' is the only one that required:
2727
* __apiKey:__ client license key
2828
* __env:__ environment name
2929
* __proxy:__ proxy server if you want to send requests via proxy
30-
* __exitOnError:__ Boolean flag indicating whether to shutdown the server after logging an uncaught exception
30+
* __exitOnError:__ boolean flag indicating whether to shutdown the server after logging an uncaught exception, defaults to false
3131

3232
#### Using with Winston
3333

@@ -47,17 +47,17 @@ All the details could be found here - [Winston Transport for Stackify](https://g
4747

4848
If you are not using Winston logger you can use default Stackify logger. It has 5 levels of messages: `trace`, `debug`, `info`, `warn` and `error`. To send the message to Stackify API you should run one of the following methods in any place of your code where you want to track some information:
4949
```js
50-
stackify.log(level, message, meta)
51-
stackify.trace(message, meta)
52-
stackify.debug(message, meta)
53-
stackify.info(message, meta)
54-
stackify.warn(message, meta)
55-
stackify.error(message, meta)
50+
stackify.log(level, message [, meta1, ... , metaN])
51+
stackify.trace(message [, meta1, ... , metaN])
52+
stackify.debug(message [, meta1, ... , metaN])
53+
stackify.info(message [, meta1, ... , metaN])
54+
stackify.warn(message [, meta1, ... , metaN])
55+
stackify.error(message [, meta1, ... , metaN])
5656
```
57+
Message must be a string
58+
meta1 ... metaN - a list of additional parameters of any type.
5759
The timestamp will be added to every message by default.
5860

59-
Meta parameter should be an object.
60-
6161
Examples of usage:
6262
```js
6363
// Add the module to all the script files where you want to log any messages.
@@ -88,7 +88,7 @@ http.createServer(function (req, res) {
8888
});
8989
});
9090
```
91-
where req is request object, instance of native NodeJS `http.IncomingMessage` object
91+
where req is request object, an instance of native NodeJS `http.IncomingMessage` object
9292

9393
You can use it also with any framework that doesn’t modify native createServer method.
9494

config/config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ module.exports = {
1414
QUEUE_CAP : 10000,
1515
MIN_BATCH_SIZE : 10,
1616
MAX_BATCH_SIZE : 100,
17-
MAX_DUP_ERROR_PER_MINUTE: 5, // Number of instances of a unique error that are allowed to be sent in one minute
17+
MAX_DUP_ERROR_PER_MINUTE: 100, // Number of instances of a unique error that are allowed to be sent in one minute
1818

1919
},
2020
IDENTIFY_DELAY: 300000, // 5 minutes delay if identifyApp call failed
2121
COOKIE_MASK: 'X-MASKED-X' // cookie headers and values mask
22+
LOGGER_VERSION: 'Node.js Stackify v.1.0',
23+
X-STACKIFY-PV: 'V1'
2224
};

lib/api.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ module.exports.methods = {
4040
};
4141

4242
// check that settings object is correct
43+
4344
if (!settings) {
4445
throw new TypeError('Settings are not provided');
4546
}
@@ -56,10 +57,11 @@ module.exports.methods = {
5657

5758
},
5859

59-
/* posting logs to API.
60-
messages - array of messages,
61-
cb - callback function
62-
shutdown - optional parameter, indicating if we should retry request attempts when API is down
60+
/*
61+
*** posting logs to API ***
62+
*** messages - array of messages ***
63+
*** cb - callback function ***
64+
*** shutdown - optional parameter, indicating if we should retry request attempts when API is down ***
6365
*/
6466
postLogs: function postLogs(messages, cb, shutdown) {
6567
var delay = 0, // scheduled delay when postLogs call failed
@@ -85,12 +87,16 @@ module.exports.methods = {
8587

8688
sender.send(opt, cb, shutdown ? null : fail);
8789
},
90+
/*
91+
*** posting logs synchronously in case if server is about to close ***
92+
*/
8893
postLogsSync: function postLogsSync(messages) {
8994
var options = {
9095
url: CONFIG.PROTOCOL + '://' + CONFIG.HOST + CONFIG.LOG_SAVE_PATH,
9196
headers : helpers.getHeaders(),
9297
data: helpers.getPostBody(messages)
9398
};
99+
console.log(options);
94100
sender.sendSync(options);
95101
}
96102
};

lib/exception.js

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var qs = require('querystring'),
33
error = require('./error'),
44
logger = require('./logger'),
55
CONFIG = require('../config/config');
6-
6+
77
module.exports = {
88
// flag used to prevent catching the same exception twice (inside and outside of the createServer method)
99
excCaught : false,
@@ -37,53 +37,46 @@ module.exports = {
3737
},
3838
// Express error handling middleware
3939
expressExceptionHandler : function expressExceptionHandler(err, req, res, next) {
40-
var cb = function () {
41-
if (CONFIG.EXIT_ON_ERROR === true) {
42-
process.exit(1);
43-
} else {
44-
next(err);
45-
}
46-
};
47-
48-
if (!err) {
49-
return next();
40+
var cb = function () {
41+
if (CONFIG.EXIT_ON_ERROR === true) {
42+
process.exit(1);
43+
} else {
44+
next(err);
5045
}
46+
};
5147

52-
if (!this.excCaught) {
53-
this.excCaught = true;
54-
logger.methods.sendException(err, req, cb);
55-
}
48+
if (!err) {
49+
return next();
50+
}
51+
52+
if (!this.excCaught) {
53+
this.excCaught = true;
54+
logger.methods.sendException(err, req, cb);
55+
}
5656
},
5757
// drain the queue and send the messages before server closes
5858
gracefulExitHandler : function gracefulExitHandler() {
59-
return (function() {
60-
61-
var drain = function drain() {
62-
if (logger.storage.length) {
63-
api.methods.postLogsSync(storage);
64-
}
65-
};
66-
59+
return (function () {
6760
// Start reading from stdin so we don't exit instantly
6861
process.stdin.resume();
6962

70-
process.on('exit', function() {
71-
drain();
63+
process.on('exit', function () {
64+
logger.methods.drain();
7265
});
7366

74-
// catch some signal events also
67+
// catch some signal events too
7568
process.on('SIGINT', function () {
76-
drain();
69+
logger.methods.drain();
7770
process.exit(1);
7871
});
7972

8073
process.on('SIGTERM', function () {
81-
drain();
74+
logger.methods.drain();
8275
process.exit(1);
8376
});
8477

8578
process.on('SIGHUP', function () {
86-
drain();
79+
logger.methods.drain();
8780
process.exit(1);
8881
});
8982
}());

lib/helpers.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@ module.exports.getURL = function getURL(req) {
117117
return href;
118118
};
119119

120-
// read the name of the app from package.json file
120+
/*
121+
*** Function for getting the name of the app from package.json file ***
122+
*/
123+
121124
module.exports.getAppName = function getAppName() {
122125
var pjsonPath = path.join(process.env.PWD, 'package.json'),
123126
pjson;
@@ -131,7 +134,10 @@ module.exports.getAppName = function getAppName() {
131134
return pjson.name;
132135
};
133136

134-
// getting post data for the request
137+
/*
138+
*** Function for getting the post data for the request ***
139+
*/
140+
135141
module.exports.getPostBody = function getPostBody(messages) {
136142
return {
137143
CDID: CONFIG.APP_DETAILS ? CONFIG.APP_DETAILS.DeviceID : null,
@@ -143,12 +149,16 @@ module.exports.getPostBody = function getPostBody(messages) {
143149
ServerName: os.hostname(),
144150
AppName: CONFIG.APPNAME,
145151
AppLoc: process.env.PWD,
146-
Logger: 'Node.js Stackify v.1.0',
152+
Logger: CONFIG.LOGGER_VERSION,
147153
Platform: 'Node.js',
148154
Msgs : messages
149155
};
150156
};
151-
//getting all the options for requests
157+
158+
/*
159+
*** Function for getting the request options ***
160+
*/
161+
152162
module.exports.getOptions = function getOptions(path, body, proxy) {
153163
var result = {
154164
url: CONFIG.PROTOCOL + '://' + CONFIG.HOST + path,
@@ -166,11 +176,15 @@ module.exports.getOptions = function getOptions(path, body, proxy) {
166176
return result;
167177

168178
};
169-
// getting request headers
179+
180+
/*
181+
*** Function for getting request headers ***
182+
*/
183+
170184
module.exports.getHeaders = function getHeaders() {
171185
return {
172186
'Content-Type': 'application/json',
173187
'X-Stackify-Key': CONFIG.APIKEY,
174-
'X-Stackify-PV': 'V1'
188+
'X-Stackify-PV': CONFIG.X-STACKIFY-PV
175189
};
176190
};

test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ app.get("/", function (req, res) {
2929
app.get("/1", function (req, res) {
3030
stackify.log('error', 'test');
3131
stackify.error('msg', {error: new Error('simple error')});
32+
stackify.error('msg', {error: new Error('simple error')});
33+
stackify.error('msg', {error: new Error('simple error')});
34+
stackify.error('msg', {error: new Error('simple error')});
35+
stackify.error('msg', {error: new Error('simple error')});
36+
stackify.error('msg', {error: new Error('simple error')});
37+
stackify.error('msg', {error: new Error('simple error')});
38+
stackify.error('msg', {error: new Error('simple error')});
39+
40+
process.exit(1);
3241
});
3342

3443
app.post("/post", function (req, res) {

0 commit comments

Comments
 (0)