Permalink
Browse files

Added "immediate" option to `logger()`. Closes #321

  • Loading branch information...
1 parent a2f8de5 commit 5f600e9870407e5db99f37aed30e7fd36923674f @tj tj committed Aug 11, 2011
Showing with 18 additions and 6 deletions.
  1. +18 −6 lib/middleware/logger.js
@@ -26,6 +26,7 @@ var defaultBufferDuration = 1000;
* - `format` Format string, see below for tokens
* - `stream` Output stream, defaults to _stdout_
* - `buffer` Buffer duration, defaults to 1000ms when _true_
+ * - `immediate` Write log line on request instead of response (for response times)
*
* Tokens:
*
@@ -88,6 +89,9 @@ exports = module.exports = function logger(options) {
options = {};
}
+ // output on request instead of response
+ var immediate = options.immediate;
+
// format name
var fmt = exports[options.format] || options.format || exports.default;
@@ -130,15 +134,23 @@ exports = module.exports = function logger(options) {
// flag as logging
req._logging = true;
- // proxy end to output loggging
- var end = res.end;
- res.end = function(chunk, encoding){
- res.end = end;
- res.end(chunk, encoding);
+ // immediate
+ if (immediate) {
var line = fmt(exports, req, res);
if (null == line) return;
stream.write(line + '\n', 'ascii');
- };
+ } else {
+ // proxy end to output loggging
+ var end = res.end;
+ res.end = function(chunk, encoding){
+ res.end = end;
+ res.end(chunk, encoding);
+ var line = fmt(exports, req, res);
+ if (null == line) return;
+ stream.write(line + '\n', 'ascii');
+ };
+ }
+
next();
};

0 comments on commit 5f600e9

Please sign in to comment.