koa-logger
Advanced tools
Comparing version 1.0.1 to 1.1.0
97
index.js
@@ -6,4 +6,5 @@ | ||
var Counter = require('passthrough-counter'); | ||
var humanize = require('humanize-number'); | ||
var bytes = require('bytes'); | ||
var ms = require('ms'); | ||
@@ -39,16 +40,40 @@ /** | ||
function dev(opts) { | ||
return function(next) { | ||
return function *dev(){ | ||
// request | ||
var start = new Date; | ||
console.log(' \033[90m<-- \033[;1m%s\033[90m %s\033[0m', this.method, this.url); | ||
try { | ||
yield next; | ||
log(this, start); | ||
} catch (err) { | ||
log(this, start, err); | ||
throw err; | ||
} | ||
return function *dev(next) { | ||
// request | ||
var start = new Date; | ||
console.log(' \033[90m<-- \033[;1m%s\033[90m %s\033[0m', this.method, this.url); | ||
try { | ||
yield next; | ||
} catch (err) { | ||
// log uncaught downstream errors | ||
log(this, start, null, err); | ||
throw err; | ||
} | ||
// calculate the length of a streaming response | ||
// by intercepting the stream with a counter. | ||
// only necessary if a content-length header is currently not set. | ||
var length = this.responseLength; | ||
var body = this.body; | ||
var counter; | ||
if (null == length && body && body.readable) { | ||
this.body = body | ||
.pipe(counter = Counter()) | ||
.on('error', this.onerror); | ||
} | ||
// log when the response is finished or closed, | ||
// whichever happens first. | ||
var ctx = this; | ||
var res = this.res; | ||
res.once('finish', done); | ||
res.once('close', done); | ||
function done(){ | ||
res.removeListener('finish', done); | ||
res.removeListener('close', done); | ||
log(ctx, start, counter ? counter.length : length); | ||
} | ||
} | ||
@@ -61,20 +86,44 @@ } | ||
function log(ctx, start, err) { | ||
function log(ctx, start, len, err) { | ||
err = err || {}; | ||
// time | ||
var delta = ms(new Date - start); | ||
// length | ||
var len = ctx.responseLength; | ||
// get the status code of the response | ||
var status = err.status || ctx.status; | ||
var handled = status != 200 || ctx.body != null; | ||
if (!handled) status = 404; | ||
var s = (err.status || ctx.status) / 100 | 0; | ||
// set the color of the status code; | ||
var s = status / 100 | 0; | ||
var c = colors[s]; | ||
// get the human readable response length | ||
var length; | ||
if (~[204, 205, 304].indexOf(status)) { | ||
length = ''; | ||
} else if (null == len) { | ||
length = '-'; | ||
} else { | ||
length = bytes(len); | ||
} | ||
console.log(' \033[90m--> \033[;1m%s\033[90m %s \033[' + c + 'm%s\033[90m %s %s\033[0m', | ||
ctx.method, | ||
ctx.url, | ||
ctx.status, | ||
delta, | ||
null == len ? '-' : bytes(len)); | ||
status, | ||
time(start), | ||
length); | ||
} | ||
/** | ||
* Show the response time in a human readable format. | ||
* In milliseconds if less than 10 seconds, | ||
* in seconds otherwise. | ||
*/ | ||
function time(start) { | ||
var delta = new Date - start; | ||
delta = delta < 10000 | ||
? delta + 'ms' | ||
: Math.round(delta / 1000) + 's'; | ||
return humanize(delta); | ||
} |
@@ -5,3 +5,3 @@ { | ||
"repository": "koajs/logger", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"keywords": [ | ||
@@ -17,9 +17,11 @@ "koa", | ||
"devDependencies": { | ||
"koa": "0.0.1" | ||
"koa": "koajs/koa", | ||
"koa-compress": "koajs/compress" | ||
}, | ||
"license": "MIT", | ||
"dependencies": { | ||
"ms": "~0.6.1", | ||
"passthrough-counter": "~0.0.1", | ||
"humanize-number": "~0.0.1", | ||
"bytes": "~0.2.1" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3382
102
3
2
+ Addedhumanize-number@~0.0.1
+ Addedpassthrough-counter@~0.0.1
+ Addedhumanize-number@0.0.2(transitive)
+ Addedpassthrough-counter@0.0.1(transitive)
- Removedms@~0.6.1
- Removedms@0.6.2(transitive)