koa-logger
Advanced tools
Comparing version 2.0.1 to 3.0.0
3.0.0 / 2017-05-16 | ||
================== | ||
* use async function | ||
* update bytes to 2.x | ||
2.0.1 / 2017-01-12 | ||
@@ -3,0 +9,0 @@ ================== |
133
index.js
/** | ||
* Module dependencies. | ||
*/ | ||
'use strict'; | ||
'use strict' | ||
const Counter = require('passthrough-counter'); | ||
const humanize = require('humanize-number'); | ||
const bytes = require('bytes'); | ||
const chalk = require('chalk'); | ||
const Counter = require('passthrough-counter') | ||
const humanize = require('humanize-number') | ||
const bytes = require('bytes') | ||
const chalk = require('chalk') | ||
/** | ||
* TTY check for dev format. | ||
*/ | ||
const isatty = process.stdout.isTTY; | ||
/** | ||
* Expose logger. | ||
*/ | ||
module.exports = dev; | ||
module.exports = dev | ||
@@ -34,3 +28,3 @@ /** | ||
0: 'yellow' | ||
}; | ||
} | ||
@@ -41,48 +35,47 @@ /** | ||
function dev(opts) { | ||
return function logger(ctx, next) { | ||
function dev (opts) { | ||
return async function logger (ctx, next) { | ||
// request | ||
const start = new Date; | ||
console.log(' ' + chalk.gray('<--') | ||
+ ' ' + chalk.bold('%s') | ||
+ ' ' + chalk.gray('%s'), | ||
const start = new Date() | ||
console.log(' ' + chalk.gray('<--') + | ||
' ' + chalk.bold('%s') + | ||
' ' + chalk.gray('%s'), | ||
ctx.method, | ||
ctx.originalUrl); | ||
ctx.originalUrl) | ||
return next().then(function() { | ||
try { | ||
await next() | ||
} catch (err) { | ||
// log uncaught downstream errors | ||
log(ctx, 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. | ||
const length = ctx.response.length; | ||
const body = ctx.body; | ||
let counter; | ||
if (null == length && body && body.readable) { | ||
ctx.body = body | ||
.pipe(counter = Counter()) | ||
.on('error', ctx.onerror); | ||
} | ||
// 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. | ||
const length = ctx.response.length | ||
const body = ctx.body | ||
let counter | ||
if (length == null && body && body.readable) { | ||
ctx.body = body | ||
.pipe(counter = Counter()) | ||
.on('error', ctx.onerror) | ||
} | ||
// log when the response is finished or closed, | ||
// whichever happens first. | ||
const res = ctx.res; | ||
// log when the response is finished or closed, | ||
// whichever happens first. | ||
const res = ctx.res | ||
const onfinish = done.bind(null, 'finish'); | ||
const onclose = done.bind(null, 'close'); | ||
const onfinish = done.bind(null, 'finish') | ||
const onclose = done.bind(null, 'close') | ||
res.once('finish', onfinish); | ||
res.once('close', onclose); | ||
res.once('finish', onfinish) | ||
res.once('close', onclose) | ||
function done(event){ | ||
res.removeListener('finish', onfinish); | ||
res.removeListener('close', onclose); | ||
log(ctx, start, counter ? counter.length : length, null, event); | ||
} | ||
}, function(err) { | ||
// log uncaught downstream errors | ||
log(ctx, start, null, err); | ||
throw err; | ||
}); | ||
function done (event) { | ||
res.removeListener('finish', onfinish) | ||
res.removeListener('close', onclose) | ||
log(ctx, start, counter ? counter.length : length, null, event) | ||
} | ||
} | ||
@@ -95,20 +88,20 @@ } | ||
function log(ctx, start, len, err, event) { | ||
function log (ctx, start, len, err, event) { | ||
// get the status code of the response | ||
const status = err | ||
? (err.status || 500) | ||
: (ctx.status || 404); | ||
: (ctx.status || 404) | ||
// set the color of the status code; | ||
const s = status / 100 | 0; | ||
const color = colorCodes[s]; | ||
const s = status / 100 | 0 | ||
const color = colorCodes[s] | ||
// get the human readable response length | ||
let length; | ||
let length | ||
if (~[204, 205, 304].indexOf(status)) { | ||
length = ''; | ||
} else if (null == len) { | ||
length = '-'; | ||
length = '' | ||
} else if (len == null) { | ||
length = '-' | ||
} else { | ||
length = bytes(len); | ||
length = bytes(len).toLowerCase() | ||
} | ||
@@ -120,8 +113,8 @@ | ||
console.log(' ' + upstream | ||
+ ' ' + chalk.bold('%s') | ||
+ ' ' + chalk.gray('%s') | ||
+ ' ' + chalk[color]('%s') | ||
+ ' ' + chalk.gray('%s') | ||
+ ' ' + chalk.gray('%s'), | ||
console.log(' ' + upstream + | ||
' ' + chalk.bold('%s') + | ||
' ' + chalk.gray('%s') + | ||
' ' + chalk[color]('%s') + | ||
' ' + chalk.gray('%s') + | ||
' ' + chalk.gray('%s'), | ||
ctx.method, | ||
@@ -131,3 +124,3 @@ ctx.originalUrl, | ||
time(start), | ||
length); | ||
length) | ||
} | ||
@@ -141,7 +134,7 @@ | ||
function time(start) { | ||
const delta = new Date - start; | ||
function time (start) { | ||
const delta = new Date() - start | ||
return humanize(delta < 10000 | ||
? delta + 'ms' | ||
: Math.round(delta / 1000) + 's'); | ||
: Math.round(delta / 1000) + 's') | ||
} |
@@ -5,3 +5,3 @@ { | ||
"repository": "koajs/logger", | ||
"version": "2.0.1", | ||
"version": "3.0.0", | ||
"keywords": [ | ||
@@ -17,6 +17,13 @@ "koa", | ||
"scripts": { | ||
"test": "mocha --harmony test.js" | ||
"lint": "eslint --fix .", | ||
"test": "mocha test.js" | ||
}, | ||
"devDependencies": { | ||
"chai": "^3.5.0", | ||
"eslint": "^3.19.0", | ||
"eslint-config-standard": "^10.2.1", | ||
"eslint-plugin-import": "^2.2.0", | ||
"eslint-plugin-node": "^4.2.2", | ||
"eslint-plugin-promise": "^3.5.0", | ||
"eslint-plugin-standard": "^3.0.1", | ||
"koa": "^2.0.0-alpha.7", | ||
@@ -31,7 +38,10 @@ "koa-route": "^3.2.0", | ||
"dependencies": { | ||
"bytes": "1", | ||
"bytes": "^2.5.0", | ||
"chalk": "^1.1.3", | ||
"humanize-number": "0.0.2", | ||
"passthrough-counter": "^1.0.0" | ||
}, | ||
"engines": { | ||
"node": ">= 7.6.0" | ||
} | ||
} |
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
5712
13
113
+ Addedbytes@2.5.0(transitive)
- Removedbytes@1.0.0(transitive)
Updatedbytes@^2.5.0