koa-logger
Advanced tools
Comparing version 1.3.0 to 2.0.0
2.0.0 / 2015-11-19 | ||
================== | ||
* add: support for koa 2 | ||
* remove: support for koa 1 | ||
1.2.2 / 2014-07-05 | ||
@@ -6,3 +12,3 @@ ================== | ||
* fix: stop using octal literals for strict mode | ||
1.2.1 / 2014-05-13 | ||
@@ -9,0 +15,0 @@ ================== |
100
index.js
/** | ||
* Module dependencies. | ||
*/ | ||
'use strict'; | ||
var Counter = require('passthrough-counter'); | ||
var humanize = require('humanize-number'); | ||
var bytes = require('bytes'); | ||
var chalk = require('chalk'); | ||
const Counter = require('passthrough-counter'); | ||
const humanize = require('humanize-number'); | ||
const bytes = require('bytes'); | ||
const chalk = require('chalk'); | ||
@@ -14,3 +15,3 @@ /** | ||
var isatty = process.stdout.isTTY; | ||
const isatty = process.stdout.isTTY; | ||
@@ -27,3 +28,3 @@ /** | ||
var colorCodes = { | ||
const colorCodes = { | ||
5: 'red', | ||
@@ -41,47 +42,47 @@ 4: 'yellow', | ||
function dev(opts) { | ||
return function *logger(next) { | ||
return function logger(ctx, next) { | ||
// request | ||
var start = new Date; | ||
const start = new Date; | ||
console.log(' ' + chalk.gray('<--') | ||
+ ' ' + chalk.bold('%s') | ||
+ ' ' + chalk.gray('%s'), | ||
this.method, | ||
this.originalUrl); | ||
ctx.method, | ||
ctx.originalUrl); | ||
try { | ||
yield next; | ||
} catch (err) { | ||
// log uncaught downstream errors | ||
log(this, start, null, err); | ||
throw err; | ||
} | ||
return next().then(function() { | ||
// 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.response.length; | ||
var body = this.body; | ||
var counter; | ||
if (null == length && body && body.readable) { | ||
this.body = body | ||
.pipe(counter = Counter()) | ||
.on('error', this.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 (null == length && body && body.readable) { | ||
ctx.body = body | ||
.pipe(counter = Counter()) | ||
.on('error', ctx.onerror); | ||
} | ||
// log when the response is finished or closed, | ||
// whichever happens first. | ||
var ctx = this; | ||
var res = this.res; | ||
// log when the response is finished or closed, | ||
// whichever happens first. | ||
const res = ctx.res; | ||
var onfinish = done.bind(null, 'finish'); | ||
var 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 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; | ||
}); | ||
} | ||
@@ -96,3 +97,3 @@ } | ||
// get the status code of the response | ||
var status = err | ||
const status = err | ||
? (err.status || 500) | ||
@@ -102,7 +103,7 @@ : (ctx.status || 404); | ||
// set the color of the status code; | ||
var s = status / 100 | 0; | ||
var color = colorCodes[s]; | ||
const s = status / 100 | 0; | ||
const color = colorCodes[s]; | ||
// get the human readable response length | ||
var length; | ||
let length; | ||
if (~[204, 205, 304].indexOf(status)) { | ||
@@ -116,3 +117,3 @@ length = ''; | ||
var upstream = err ? chalk.red('xxx') | ||
const upstream = err ? chalk.red('xxx') | ||
: event === 'close' ? chalk.yellow('-x-') | ||
@@ -141,7 +142,6 @@ : chalk.gray('-->') | ||
function time(start) { | ||
var delta = new Date - start; | ||
delta = delta < 10000 | ||
const delta = new Date - start; | ||
return humanize(delta < 10000 | ||
? delta + 'ms' | ||
: Math.round(delta / 1000) + 's'; | ||
return humanize(delta); | ||
: Math.round(delta / 1000) + 's'); | ||
} |
@@ -5,3 +5,3 @@ { | ||
"repository": "koajs/logger", | ||
"version": "1.3.0", | ||
"version": "2.0.0", | ||
"keywords": [ | ||
@@ -21,4 +21,4 @@ "koa", | ||
"chai": "^2.3.0", | ||
"koa": "^0.21.0", | ||
"koa-route": "^2.4.1", | ||
"koa": "^2.0.0-alpha.3", | ||
"koa-route": "^3.0.0", | ||
"mocha": "^2.2.5", | ||
@@ -25,0 +25,0 @@ "sinon": "^1.14.1", |
@@ -9,2 +9,4 @@ | ||
___Notice: `koa-logger@2` supports `koa@2`; if you want to use this module with `koa@1`, please use `koa-logger@1`.___ | ||
``` | ||
@@ -30,6 +32,6 @@ <-- GET / | ||
```js | ||
var logger = require('koa-logger') | ||
var koa = require('koa') | ||
const logger = require('koa-logger') | ||
const koa = require('koa') | ||
var app = koa() | ||
const app = new Koa() | ||
app.use(logger()) | ||
@@ -36,0 +38,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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
5388
51
115
1