8000 fix bug: headers are changed after log entry emits · yanxi123-com/log4js-node@cd3971c · GitHub
[go: up one dir, main page]

Skip to content

Commit cd3971c

Browse files
committed
fix bug: headers are changed after log entry emits
In the original version, the following operation looks synchronic, however it is not: ``` res.end = end; res.end(data,enc); //emit the log entry ``` In fact, it starts a series of async operations, in which the request may yet be changed after the request log has already been emitted (in our case - a change on request headers was observed, probably by some low level hook or hacky wrap of some http.ServerResponse method that's involved on the process - but called asynchronously) what leads to situation that the request log does not capture valid data. (observed by us: - request headers - calculated end time, when concerning big content ) The fix just used `setTimeout(function() { /*emit the log entry*/ }, 1)`, but I'm afraid it may not hold true for big contents. Well. maybe the headers part will, but the response time calculation will lie. The fix relays on: http://nodejs.org/api/http.html#http_event_finish
1 parent 176d448 commit cd3971c

File tree

1 file changed

+3
-6
lines changed

1 file changed

+3
-6
lines changed

lib/connect-logger.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ function getLogger(logger4js, options) {
5656
var start = new Date()
5757
, statusCode
5858
, writeHead = res.writeHead
59-
, end = res.end
6059
, url = req.originalUrl;
6160

6261
// flag as logging
@@ -79,10 +78,8 @@ function getLogger(logger4js, options) {
7978
}
8079
};
8180

82-
// proxy end to output a line to the provided logger.
83-
res.end = function(chunk, encoding) {
84-
res.end = end;
85-
res.end(chunk, encoding);
81+
//hook on end request to emit the log entry of the HTTP request.
82+
res.on('finish', function() {
8683
res.responseTime = new Date() - start;
8784
//status code response level handling
8885
if(res.statusCode && options.level === 'auto'){
@@ -98,7 +95,7 @@ function getLogger(logger4js, options) {
9895
thislogger.log(level, format(fmt, req, res));
9996
}
10097
}
101-
};
98+
});
10299
}
103100

104101
//ensure next gets always called

0 commit comments

Comments
 (0)
0