0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-02-17 23:45:29 -05:00

logging didn't work on chunked output

This commit is contained in:
Alex Kocharin 2013-10-19 01:53:27 +04:00
parent fa51797602
commit acc2e571ff

View file

@ -100,13 +100,12 @@ module.exports.log_and_etagify = function(req, res, next) {
var _send = res.send; var _send = res.send;
res.send = function(body) { res.send = function(body) {
var error;
if (typeof(body) === 'string' || typeof(body) === 'object') { if (typeof(body) === 'string' || typeof(body) === 'object') {
res.header('Content-type', 'application/json'); res.header('Content-type', 'application/json');
if (typeof(body) === 'object' && body != null) { if (typeof(body) === 'object' && body != null) {
if (body.error) { if (body.error) {
error = body.error; res._sinopia_error = body.error;
} }
body = JSON.stringify(body, undefined, '\t'); body = JSON.stringify(body, undefined, '\t');
} }
@ -116,8 +115,24 @@ module.exports.log_and_etagify = function(req, res, next) {
// send(null), send(204), etc. // send(null), send(204), etc.
} }
res.send = _send;
res.send(body);
}
var bytesout = 0
var _write = res.write
res.write = function(buf) {
bytesout += buf.length
_write.apply(res, arguments)
}
var _end = res.end;
res.end = function(buf) {
if (buf) bytesout += buf.length
_end.apply(res, arguments)
var msg = '@{status}, user: @{user}, req: \'@{request.method} @{request.url}\''; var msg = '@{status}, user: @{user}, req: \'@{request.method} @{request.url}\'';
if (error) { if (res._sinopia_error) {
msg += ', error: @{!error}'; msg += ', error: @{!error}';
} else { } else {
msg += ', bytes: @{bytes.in}/@{bytes.out}'; msg += ', bytes: @{bytes.in}/@{bytes.out}';
@ -127,15 +142,12 @@ module.exports.log_and_etagify = function(req, res, next) {
level: 35, // http level: 35, // http
user: req.user, user: req.user,
status: res.statusCode, status: res.statusCode,
error: error, error: res._sinopia_error,
bytes: { bytes: {
in: bytesin, in: bytesin,
out: body.length, out: bytesout,
} }
}, msg); }, msg);
res.send = _send;
res.send(body);
}; };
next(); next();
} }