koa-bunyan-logger
Advanced tools
Comparing version
2.0.0-pre1 / 2016-04-15 | ||
======================= | ||
* Support koa v2 #8 #9 [ifraixedes](https://github.com/ifraixedes) | ||
1.3.0 / 2015-10-26 | ||
@@ -3,0 +8,0 @@ ================== |
74
index.js
@@ -36,6 +36,6 @@ 'use strict'; | ||
return function *logger(next) { | ||
this.log = loggerInstance; | ||
return function logger(ctx, next) { | ||
ctx.log = loggerInstance; | ||
yield *next; // jshint ignore:line | ||
return next(); | ||
}; | ||
@@ -67,7 +67,7 @@ }; | ||
return function *requestIdContext(next) { | ||
var reqId = this.request.get(header) || uuid.v4(); | ||
return function requestIdContext(ctx, next) { | ||
var reqId = ctx.request.get(header) || uuid.v4(); | ||
this[ctxProp] = reqId; | ||
this.request[requestProp] = reqId; | ||
ctx[ctxProp] = reqId; | ||
ctx.request[requestProp] = reqId; | ||
@@ -77,9 +77,9 @@ var logFields = {}; | ||
if (!this.log) { | ||
if (!ctx.log) { | ||
throw new Error('must use(koaBunyanLogger()) before this middleware'); | ||
} | ||
this.log = this.log.child(logFields); | ||
ctx.log = ctx.log.child(logFields); | ||
yield *next; // jshint ignore:line | ||
return next(); | ||
}; | ||
@@ -129,13 +129,13 @@ }; | ||
return function *requestLogger(next) { | ||
var url = this.url; | ||
return function requestLogger(ctx, next) { | ||
var url = ctx.url; | ||
var requestData = { | ||
req: this.req | ||
req: ctx.req | ||
}; | ||
requestData = updateFields(this, opts.updateLogFields, requestData); | ||
requestData = updateFields(this, opts.updateRequestLogFields, requestData); | ||
requestData = updateFields(ctx, opts.updateLogFields, requestData); | ||
requestData = updateFields(ctx, opts.updateRequestLogFields, requestData); | ||
this.log.info(requestData, formatRequestMessage.call(this, requestData)); | ||
ctx.log.info(requestData, formatRequestMessage.call(ctx, requestData)); | ||
@@ -147,4 +147,4 @@ var startTime = new Date().getTime(); | ||
var responseData = { | ||
req: this.req, | ||
res: this.res | ||
req: ctx.req, | ||
res: ctx.res | ||
}; | ||
@@ -158,28 +158,26 @@ | ||
responseData = updateFields(this, opts.updateLogFields, responseData); | ||
responseData = updateFields(this, opts.updateResponseLogFields, | ||
responseData = updateFields(ctx, opts.updateLogFields, responseData); | ||
responseData = updateFields(ctx, opts.updateResponseLogFields, | ||
responseData, err); | ||
var level = levelFn.call(this, this.status, err); | ||
var level = levelFn.call(ctx, ctx.status, err); | ||
this.log[level](responseData, | ||
formatResponseMessage.call(this, responseData)); | ||
ctx.log[level](responseData, | ||
formatResponseMessage.call(ctx, responseData)); | ||
// Remove log object to mitigate accidental leaks | ||
this.log = null; | ||
ctx.log = null; | ||
}; | ||
try { | ||
yield *next; // jshint ignore:line | ||
} catch (e) { | ||
return next().catch(e => { | ||
err = e; | ||
} finally { | ||
}).then(() => { // Emulate a finally | ||
// Handle response logging and cleanup when request is finished | ||
// This ensures that the default error handler is done | ||
onFinished(this.response.res, onResponseFinished.bind(this)); | ||
} | ||
onFinished(ctx.response.res, onResponseFinished.bind(ctx)); | ||
if (err) { | ||
throw err; // rethrow | ||
} | ||
if (err) { | ||
throw err; // rethrow | ||
} | ||
}); | ||
}; | ||
@@ -222,9 +220,9 @@ }; | ||
return function *timeContext(next) { | ||
this._timeContextStartTimes = {}; | ||
return function timeContext(ctx, next) { | ||
ctx._timeContextStartTimes = {}; | ||
this.time = time; | ||
this.timeEnd = timeEnd; | ||
ctx.time = time; | ||
ctx.timeEnd = timeEnd; | ||
yield* next; // jshint ignore:line | ||
return next(); | ||
}; | ||
@@ -231,0 +229,0 @@ |
{ | ||
"name": "koa-bunyan-logger", | ||
"version": "1.3.0", | ||
"version": "2.0.0-pre1", | ||
"description": "Koa middleware for logging requests using bunyan", | ||
"main": "index.js", | ||
"engines": { | ||
"node": ">= 0.11.4" | ||
"node": ">= 4.x" | ||
}, | ||
@@ -13,3 +13,2 @@ "scripts": { | ||
"keywords": [ | ||
"co", | ||
"koa", | ||
@@ -22,8 +21,8 @@ "bunyan", | ||
"type": "git", | ||
"url": "https://github.com/pebble/koa-bunyan-logger.git" | ||
"url": "https://github.com/koajs/bunyan-logger.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/pebble/koa-bunyan-logger/issues" | ||
"url": "https://github.com/koajs/bunyan-logger/issues" | ||
}, | ||
"homepage": "https://github.com/pebble/koa-bunyan-logger", | ||
"homepage": "https://github.com/koajs/bunyan-logger", | ||
"author": "Pebble Technology <webteam@getpebble.com>", | ||
@@ -41,3 +40,3 @@ "license": "MIT", | ||
"jshint": "~2.5.8", | ||
"koa": "~1.1.1", | ||
"koa": "^2.0.0", | ||
"mocha": "~2.3.3", | ||
@@ -44,0 +43,0 @@ "supertest": "~1.1.0" |
@@ -1,3 +0,5 @@ | ||
# koa-bunyan-logger | ||
# bunyan-logger | ||
NOTE that you're reading the README of the version which targets [Koa v2.x](https://github.com/koajs/koa/tree/v2.x), if you want to read the current version read the README in master branch | ||
Flexible log context and request logging middleware | ||
@@ -10,4 +12,4 @@ for [koa](http://koajs.com/) using [bunyan](https://github.com/trentm/node-bunyan). | ||
[](https://travis-ci.org/pebble/koa-bunyan-logger) | ||
[](https://coveralls.io/r/pebble/koa-bunyan-logger) | ||
[](https://travis-ci.org/koajs/bunyan-logger) | ||
[](https://coveralls.io/r/koajs/bunyan-logger) | ||
[](https://www.npmjs.org/package/koa-bunyan-logger) | ||
@@ -26,4 +28,4 @@ | ||
app.use(function *() { | ||
this.log.info({'Got a request from %s for %s', this.request.ip, this.path); | ||
app.use(function (ctx) { | ||
ctx.log.info({'Got a request from %s for %s', this.request.ip, this.path); | ||
}); | ||
@@ -36,3 +38,3 @@ | ||
``` | ||
node --harmony examples/simple.js | ./node_modules/.bin/bunyan -o short` | ||
node examples/simple.js | ./node_modules/.bin/bunyan -o short` | ||
``` | ||
@@ -60,3 +62,3 @@ | ||
``` | ||
node --harmony examples/requests.js | ./node_modules/.bin/bunyan -o short | ||
node examples/requests.js | ./node_modules/.bin/bunyan -o short | ||
``` | ||
@@ -236,5 +238,5 @@ | ||
app.use(function * () { | ||
this.response.set('X-Server-Request-Id', this.reqId); | ||
this.body = "Hello world"; | ||
app.use(function (ctx) { | ||
ctx.response.set('X-Server-Request-Id', this.reqId); | ||
ctx.body = "Hello world"; | ||
}); | ||
@@ -267,14 +269,43 @@ ``` | ||
app.use(function * () { | ||
this.time('get data'); | ||
var user = yield getUser(); | ||
var friends = yield getFriend(user); | ||
this.timeEnd('get data'); | ||
app.use(function (ctx) { | ||
ctx.time('get data'); | ||
this.time('serialize'); | ||
this.body = serialize(user, friends); | ||
this.timeEnd('serialize'); | ||
return getUser() | ||
.then(u => { | ||
return getFriend(u) | ||
.then(f => [u, f]); | ||
}) | ||
.then(data => { | ||
let user = data[0]; | ||
let friends = data[1]; | ||
ctx.timeEnd('get data'); | ||
ctx.time('serialize'); | ||
ctx.body = serialize(user, friends); | ||
ctx.timeEnd('serialize'); | ||
}); | ||
}); | ||
``` | ||
The same but using async functions | ||
```js | ||
var koaBunyanLogger = require('koa-bunyan-logger'); | ||
app.use(koaBunyanLogger()); | ||
app.use(koaBunyanLogger.requestIdContext()); | ||
app.use(koaBunyanLogger.timeContext()); | ||
app.use(async function (ctx) { | ||
ctx.time('get data'); | ||
let user = await getUser(); | ||
let friends = await getFriend(user); | ||
ctx.timeEnd('get data'); | ||
ctx.time('serialize'); | ||
ctx.body = serialize(user, friends); | ||
ctx.timeEnd('serialize'); | ||
}); | ||
``` | ||
Example output: | ||
@@ -317,7 +348,6 @@ ```json | ||
[Pebble Technology!](https://getpebble.com) | ||
[Pebble Technology!](https://www.pebble.com) | ||
## License | ||
[MIT](https://github.com/pebble/koa-bunyan-logger/blob/master/LICENSE) | ||
[MIT](https://github.com/koajs/bunyan-logger/blob/master/LICENSE) |
@@ -1,2 +0,2 @@ | ||
var koa = require('koa'); | ||
var Koa = require('koa'); | ||
var koaBunyanLogger = require('../'); | ||
@@ -16,3 +16,3 @@ var supertest = require('supertest'); | ||
beforeEach(function *() { | ||
app = koa(); | ||
app = new Koa(); | ||
app.on('error', function () {}); // suppress errors | ||
@@ -53,4 +53,4 @@ | ||
var helloWorld = function *() { | ||
this.body = 'Hello world'; | ||
var helloWorld = function (ctx) { | ||
ctx.body = 'Hello world'; | ||
}; | ||
@@ -60,5 +60,5 @@ | ||
app.use(koaBunyanLogger()); | ||
app.use(function *() { | ||
assert.ok(this.log); | ||
this.body = ''; | ||
app.use(function (ctx) { | ||
assert.ok(ctx.log); | ||
ctx.body = ''; | ||
}); | ||
@@ -72,5 +72,5 @@ | ||
app.use(function *() { | ||
this.log.info('Got request'); | ||
this.body = 'Hello world'; | ||
app.use(function (ctx) { | ||
ctx.log.info('Got request'); | ||
ctx.body = 'Hello world'; | ||
}); | ||
@@ -87,3 +87,3 @@ | ||
beforeEach(function *() { | ||
beforeEach(function () { | ||
app.use(koaBunyanLogger(ringLogger)); | ||
@@ -111,4 +111,4 @@ }); | ||
app.use(function *() { | ||
this.throw(404); | ||
app.use(function (ctx) { | ||
ctx.throw(404); | ||
}); | ||
@@ -124,3 +124,3 @@ | ||
app.use(function *() { | ||
app.use(function () { | ||
throw new Error('oh no'); | ||
@@ -157,3 +157,3 @@ }); | ||
app.use(function *() { | ||
app.use(function () { | ||
throw new Error('uh oh'); | ||
@@ -210,5 +210,5 @@ }); | ||
app.use(function *() { | ||
this.log.info('hello world'); | ||
this.body = ""; | ||
app.use(function (ctx) { | ||
ctx.log.info('hello world'); | ||
ctx.body = ""; | ||
}); | ||
@@ -225,5 +225,5 @@ | ||
app.use(function *() { | ||
this.log.info('hello world'); | ||
this.body = ""; | ||
app.use(function (ctx) { | ||
ctx.log.info('hello world'); | ||
ctx.body = ""; | ||
}); | ||
@@ -242,6 +242,6 @@ | ||
app.use(function *() { | ||
this.time('foo'); | ||
this.timeEnd('foo'); | ||
this.body = ''; | ||
app.use(function (ctx) { | ||
ctx.time('foo'); | ||
ctx.timeEnd('foo'); | ||
ctx.body = ''; | ||
}); | ||
@@ -258,8 +258,8 @@ | ||
app.use(function *() { | ||
this.time('foo'); | ||
this.time('bar'); | ||
this.timeEnd('bar'); | ||
this.timeEnd('foo'); | ||
this.body = ''; | ||
app.use(function (ctx) { | ||
ctx.time('foo'); | ||
ctx.time('bar'); | ||
ctx.timeEnd('bar'); | ||
ctx.timeEnd('foo'); | ||
ctx.body = ''; | ||
}); | ||
@@ -278,6 +278,6 @@ | ||
app.use(function *() { | ||
this.time('x'); | ||
this.time('x'); | ||
this.body = ''; | ||
app.use(function (ctx) { | ||
ctx.time('x'); | ||
ctx.time('x'); | ||
ctx.body = ''; | ||
}); | ||
@@ -294,5 +294,5 @@ | ||
app.use(function *() { | ||
this.timeEnd('blam'); | ||
this.body = ''; | ||
app.use(function (ctx) { | ||
ctx.timeEnd('blam'); | ||
ctx.body = ''; | ||
}); | ||
@@ -318,6 +318,6 @@ | ||
app.use(function *() { | ||
this.time('foo'); | ||
this.timeEnd('foo'); | ||
this.body = ''; | ||
app.use(function (ctx) { | ||
ctx.time('foo'); | ||
ctx.timeEnd('foo'); | ||
ctx.body = ''; | ||
}); | ||
@@ -324,0 +324,0 @@ |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
30495
2.41%347
9.46%531
-0.38%1
Infinity%