Skip to content
This repository
Browse code

Fixed logger to support mess of writeHead()/progressive api for node …

…0.4.x
  • Loading branch information...
commit 8fd9a1f808f911997ae67524071cd313c5dc931c 1 parent de9fe66
TJ Holowaychuk authored
12  examples/logger.format.js
@@ -5,13 +5,17 @@
5 5
 
6 6
 var connect = require('../');
7 7
 
8  
-// $ curl -i http://localhost:3000/favicon.ico
  8
+// $ curl -i http://localhost:3000/
9 9
 
10 10
 // custom format with ansi-escape sequence colors
11 11
 // format: <method> <url> <status> <response-time>ms
12 12
 
13 13
 
14 14
 connect.createServer(
15  
-    connect.logger('\033[90m:method\033[0m \033[36m:url\033[0m \033[90m:status :response-timems\033[0m')
16  
-  , connect.favicon()
17  
-).listen(3000);
  15
+    connect.logger('\033[90m:method\033[0m \033[36m:url\033[0m \033[90m:status :response-timems -> :res[Content-Type]\033[0m')
  16
+  , function(req, res){
  17
+    res.statusCode = 500;
  18
+    res.setHeader('Content-Type', 'text/plain');
  19
+    res.end('Internal Server Error');
  20
+  }
  21
+).listen(3000);
3  examples/logger.js
@@ -10,4 +10,5 @@ var connect = require('../');
10 10
 connect.createServer(
11 11
     connect.logger()
12 12
   , connect.favicon()
13  
-).listen(3000);
  13
+).listen(3000);
  14
+
23  lib/middleware/logger.js
@@ -83,7 +83,6 @@ module.exports = function logger(options) {
83 83
   return function logger(req, res, next) {
84 84
     var start = +new Date
85 85
       , statusCode
86  
-      , resHeaders
87 86
       , writeHead = res.writeHead
88 87
       , end = res.end
89 88
       , url = req.originalUrl;
@@ -98,8 +97,8 @@ module.exports = function logger(options) {
98 97
     res.writeHead = function(code, headers){
99 98
       res.writeHead = writeHead;
100 99
       res.writeHead(code, headers);
101  
-      res._statusCode = statusCode = code;
102  
-      res._headers = resHeaders = headers || {};
  100
+      res.__statusCode = statusCode = code;
  101
+      res.__headers = headers || {};
103 102
     };
104 103
 
105 104
     // proxy end to output a line to the provided logger.
@@ -112,6 +111,10 @@ module.exports = function logger(options) {
112 111
       };
113 112
     } else {
114 113
       res.end = function(chunk, encoding) {
  114
+        var contentLength = (res._headers && res._headers['content-length'])
  115
+          || (res.__headers && res.__headers['Content-Length'])
  116
+          || '-';
  117
+
115 118
         res.end = end;
116 119
         res.end(chunk, encoding);
117 120
 
@@ -119,7 +122,7 @@ module.exports = function logger(options) {
119 122
            + ' - - [' + (new Date).toUTCString() + ']'
120 123
            + ' "' + req.method + ' ' + url
121 124
            + ' HTTP/' + req.httpVersionMajor + '.' + req.httpVersionMinor + '" '
122  
-           + statusCode + ' ' + (resHeaders['Content-Length'] || '-')
  125
+           + (statusCode || res.statusCode) + ' ' + contentLength
123 126
            + ' "' + (req.headers['referer'] || req.headers['referrer'] || '')
124 127
            + '" "' + (req.headers['user-agent'] || '') + '"\n', 'ascii');
125 128
       };
@@ -143,13 +146,17 @@ function format(str, req, res) {
143 146
   return str
144 147
     .replace(':url', req.originalUrl)
145 148
     .replace(':method', req.method)
146  
-    .replace(':status', res._statusCode)
  149
+    .replace(':status', res.__statusCode || res.statusCode)
147 150
     .replace(':response-time', res.responseTime)
148 151
     .replace(':date', new Date().toUTCString())
149 152
     .replace(':referrer', req.headers['referer'] || req.headers['referrer'] || '')
150 153
     .replace(':http-version', req.httpVersionMajor + '.' + req.httpVersionMinor)
151 154
     .replace(':remote-addr', req.socket && req.socket.remoteAddress)
152 155
     .replace(':user-agent', req.headers['user-agent'] || '')
153  
-    .replace(/:req\[([^\]]+)\]/g, function(_, header){ return req.headers[header]; })
154  
-    .replace(/:res\[([^\]]+)\]/g, function(_, header){ return res._headers[header]; });
155  
-}
  156
+    .replace(/:req\[([^\]]+)\]/g, function(_, field){ return req.headers[field.toLowerCase()]; })
  157
+    .replace(/:res\[([^\]]+)\]/g, function(_, field){
  158
+      return res._headers
  159
+        ? (res._headers[field.toLowerCase()] || res.__headers[field])
  160
+        : (res.__headers && res.__headers[field]);
  161
+    });
  162
+}

0 notes on commit 8fd9a1f

Please sign in to comment.
Something went wrong with that request. Please try again.