Skip to content

Commit 22960e9

Browse files
alexk-blackopsalexk-blackops
authored andcommitted
different fixes
1 parent d72cae3 commit 22960e9

File tree

9 files changed

+72
-68
lines changed

9 files changed

+72
-68
lines changed

README.md

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ The following options could be passed. 'apiKey' is the only one that required:
2929
* __exitOnError:__ boolean flag indicating whether to shutdown the server after logging an uncaught exception, defaults to false
3030
* __proxy:__ proxy server if you want to send requests via proxy.
3131

32-
*Notice:* stackify-logger sends synchronous requests before any `process.exit()` calls in your code. Sending via proxy wouldn't be possible in this case.
32+
*Notice:* stackify-logger sends synchronous requests if you call `process.exit()`. Sending via proxy wouldn't be possible in this case.
3333

3434
#### Using with Winston
3535

@@ -62,8 +62,6 @@ stackify.error(message [, meta1, ... , metaN])
6262

6363
**meta1 ... metaN** - a list of additional parameters of any type.
6464

65-
The timestamp will be added to every message by default.
66-
6765
Examples of usage:
6866
```js
6967
// Add the module to all the script files where you want to log any messages.
@@ -75,33 +73,15 @@ stackify.info('any message', {anything: 'this is metadata'});
7573
stackify.warn('attention');
7674
stackify.log('error', {error : new Error()});
7775
```
78-
When logging an error message you could pass an Error object in metadata like in the last case so the exception details would be available.
76+
When logging an error message you can pass an Error object in metadata like in the last example, so the exception details would be available.
7977

8078
#### Exception handling
81-
By executing `stackify.start()` you set handler for uncaught exceptions.
82-
Be sure to run it before any methods that set exception handlers.
83-
84-
##### Using with pure NodeJS app
85-
If you want to get web details of an exception to be sent with it you should run `stackify.exceptionHandler(req)` first line inside of native `createServer` method :
86-
87-
```js
88-
var http = require('http');
89-
var stackify = require('stackify-logger');
90-
http.createServer(function (req, res) {
91-
stackify.exceptionHandler(req);
92-
res.setHeader('content-type', 'text/plain');
93-
res.end('hello');
94-
});
95-
});
96-
```
97-
where req is request object, an instance of native NodeJS `http.IncomingMessage` object
98-
99-
You can use it also with any framework that doesn’t modify native createServer method.
100-
79+
By executing `stackify.start()` you set a handler for uncaught exceptions.
80+
Make sure you run it before any methods that set exception handlers.
10181

10282
##### Using with Express
10383
Global handler doesn't work inside Express route methods.
104-
You should use error-handling middleware function `stackify.expressExceptionHandler`. Since middleware is executed serially, it's order of inclusion is important. Be sure to add it before any other error-handling middleware.
84+
You should use error-handling middleware function `stackify.expressExceptionHandler`. Since middleware is executed serially, it's order of inclusion is important. Make sure you add it before any other error-handling middleware.
10585

10686
```js
10787
var express = require('express');

config/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = {
1414
QUEUE_CAP : 10000,
1515
MIN_BATCH_SIZE : 10,
1616
MAX_BATCH_SIZE : 100,
17-
MAX_DUP_ERROR_PER_MINUTE: 100, // 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

index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module.exports = {
66
// start sending logs
77
start: function (options) {
88
api.methods.identifyApp(options);
9-
exception.exceptionHandler(null, options.exitOnError);
9+
exception.catchException(options.exitOnError || false);
1010
exception.gracefulExitHandler();
1111
},
1212

@@ -16,9 +16,9 @@ module.exports = {
1616
info: logger.methods.info,
1717
warn: logger.methods.warn,
1818
error: logger.methods.error,
19-
19+
20+
//common method for handling logged messages
2021
push: logger.methods.push,
2122

22-
exceptionHandler: exception.exceptionHandler,
2323
expressExceptionHandler: exception.expressExceptionHandler
2424
};

lib/api.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,15 @@ module.exports.methods = {
8787

8888
sender.send(opt, cb, shutdown ? null : fail);
8989
},
90-
/*
91-
*** posting logs synchronously in case if server is about to close ***
90+
/*
91+
*** posting logs synchronously in case if server is about to close ***
9292
*/
9393
postLogsSync: function postLogsSync(messages) {
9494
var options = {
9595
url: CONFIG.PROTOCOL + '://' + CONFIG.HOST + CONFIG.LOG_SAVE_PATH,
9696
headers : helpers.getHeaders(),
9797
data: helpers.getPostBody(messages)
9898
};
99-
console.log(options);
10099
sender.sendSync(options);
101100
}
102101
};

lib/error.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ module.exports = {
1515
checkErrorLimitMessage : function checkErrorLimitMessage(ex) {
1616
var d = new Date(),
1717
min = d.getFullYear().toString() + d.getMonth().toString() + d.getDate().toString()
18-
+ d.getHours().toString() + d.getMinutes().toString();
18+
+ d.getHours().toString() + d.getMinutes().toString(),
19+
key;
1920

2021
if (!ex) {
2122
return true;

lib/exception.js

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,23 @@
1-
var qs = require('querystring'),
2-
3-
error = require('./error'),
1+
var error = require('./error'),
42
logger = require('./logger'),
53
CONFIG = require('../config/config');
64

75
module.exports = {
86
// flag used to prevent catching the same exception twice (inside and outside of the createServer method)
97
excCaught : false,
10-
// Pure Node apps exception catching
11-
exceptionHandler : function exceptionHandler(req, exit) {
8+
9+
// General exception handler
10+
catchException : function catchException(exit) {
1211
var self = this;
13-
CONFIG.EXIT_ON_ERROR = CONFIG.EXIT_ON_ERROR || exit;
14-
return (function () {
15-
var body = '';
16-
if (req) {
17-
req.on('data', function (chunk) {
18-
body += chunk;
19-
});
20-
req.on('end', function () {
21-
var json = qs.parse(body);
22-
req.body = json;
23-
});
24-
}
2512

13+
CONFIG.EXIT_ON_ERROR = exit;
14+
15+
return (function () {
2616
process.on('uncaughtException', function (err) {
2717
if (!self.excCaught) {
2818
self.excCaught = true;
29-
logger.methods.sendException(err, req, function () {
30-
if (CONFIG.EXIT_ON_ERROR === true) {
19+
logger.methods.sendException(err, null, function () {
20+
if (exit === true) {
3121
process.exit(1);
3222
}
3323
});
@@ -56,29 +46,38 @@ module.exports = {
5646
},
5747
// drain the queue and send the messages before server closes
5848
gracefulExitHandler : function gracefulExitHandler() {
49+
5950
return (function () {
51+
// if any signal has been caught
52+
var exit = false,
53+
shutdown = function () {
54+
logger.methods.drain();
55+
exit = true;
56+
process.exit(1);
57+
};
58+
6059
// Start reading from stdin so we don't exit instantly
6160
process.stdin.resume();
6261

6362
process.on('exit', function () {
64-
logger.methods.drain();
63+
if (!exit) {
64+
logger.methods.drain();
65+
}
6566
});
6667

6768
// catch some signal events too
6869
process.on('SIGINT', function () {
69-
logger.methods.drain();
70-
process.exit(1);
70+
shutdown();
7171
});
7272

7373
process.on('SIGTERM', function () {
74-
logger.methods.drain();
75-
process.exit(1);
74+
shutdown();
7675
});
7776

7877
process.on('SIGHUP', function () {
79-
logger.methods.drain();
80-
process.exit(1);
78+
shutdown();
8179
});
8280
}());
83-
},
81+
}
82+
8483
};

lib/helpers.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var stackTrace = require('stack-trace'),
22

33
path = require('path'),
44
os = require('os'),
5+
qs = require('querystring'),
56

67
CONFIG = require('../config/config');
78

@@ -183,8 +184,30 @@ module.exports.getOptions = function getOptions(path, body, proxy) {
183184

184185
module.exports.getHeaders = function getHeaders() {
185186
return {
187+
'Accept': 'application/json',
186188
'Content-Type': 'application/json',
187189
'X-Stackify-Key': CONFIG.APIKEY,
188190
'X-Stackify-PV': CONFIG.X_STACKIFY_PV
189191
};
192+
};
193+
/*
194+
*** Function for getting post data from the request
195+
*/
196+
module.exports.getPostData = function getPostData (req) {
197+
return (function() {
198+
if (request.method == 'POST') {
199+
var body = '';
200+
request.on('data', function (data) {
201+
body += data;
202+
203+
// Too much POST data, kill the connection!
204+
if (body.length > 1e6)
205+
request.connection.destroy();
206+
});
207+
request.on('end', function () {
208+
var json = qs.parse(body);
209+
req.body = json;
210+
});
211+
}
212+
}());
190213
};

lib/sender.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,14 @@ module.exports.send = function send(options, cb, fail) {
3535

3636
module.exports.sendSync = function sendSync(options) {
3737

38-
var res = requestSync('POST', options.url, {
39-
json: options.data,
40-
headers: options.headers
41-
});
38+
try {
39+
var res = requestSync('POST', options.url, {
40+
json: options.data,
41+
headers: options.headers
42+
});
43+
} catch (err) {
44+
console.error(err.stack);
45+
process.exit(1);
46+
}
4247

4348
};

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,8 @@
1919
},
2020
"homepage": "https://github.com/stackify/stackify-log-nodejs",
2121
"dependencies": {
22-
"http-sync": "0.0.5",
23-
"pkginfo": "~0.3.x",
2422
"request": "~2.48.x",
2523
"stack-trace": "0.0.9",
26-
"sync-request": "^1.0.1",
27-
"urllib-sync": "^1.0.1"
24+
"sync-request": "^1.0.1"
2825
}
2926
}

0 commit comments

Comments
 (0)