express-bunyan-logger
Advanced tools
Comparing version 1.0.0 to 1.1.0
58
index.js
var bunyan = require('bunyan'), | ||
useragent = require('useragent'), | ||
uuid = require('node-uuid'), | ||
util = require('util'); | ||
@@ -19,2 +20,3 @@ | ||
excludes, | ||
genReqId = defaultGenReqId, | ||
levelFn = defaultLevelFn; | ||
@@ -48,5 +50,31 @@ | ||
if (opts.genReqId) { | ||
genReqId = typeof genReqId == 'function' ? opts.genReqId : defaultGenReqId; | ||
}else if (opts.hasOwnProperty('genReqId')) { | ||
genReqId = false; | ||
} | ||
return function (err, req, res, next) { | ||
var startTime = Date.now(); | ||
var app = req.app || res.app; | ||
if (!logger) { | ||
opts.name = (opts.name || app.settings.shortname || app.settings.name || app.settings.title || 'express'); | ||
opts.serializers = opts.serializers || {}; | ||
opts.serializers.req = opts.serializers.req || bunyan.stdSerializers.req; | ||
opts.serializers.res = opts.serializers.res || bunyan.stdSerializers.res; | ||
err && (opts.serializers.err = opts.serializers.err || bunyan.stdSerializers.err); | ||
logger = bunyan.createLogger(opts); | ||
} | ||
var requestId; | ||
if (genReqId) | ||
requestId = genReqId(req); | ||
var childLogger = requestId !== undefined ? logger.child({req_id: requestId}) : logger; | ||
req.log = childLogger; | ||
function logging(incoming) { | ||
@@ -58,6 +86,5 @@ if (!incoming) { | ||
var app = req.app || res.app, | ||
status = res.statusCode, | ||
var status = res.statusCode, | ||
method = req.method, | ||
url = req.url || '-', | ||
url = (req.baseUrl || '') + (req.url || '-'), | ||
referer = req.header('referer') || req.header('referrer') || '-', | ||
@@ -69,15 +96,4 @@ ua = parseUA ? useragent.parse(req.header('user-agent')) : req.header('user-agent'), | ||
if (!logger) { | ||
opts.name = (opts.name || app.settings.shortname || app.settings.name || app.settings.title || 'express'); | ||
opts.serializers = opts.serializers || {}; | ||
opts.serializers.req = opts.serializers.req || bunyan.stdSerializers.req; | ||
opts.serializers.res = opts.serializers.res || bunyan.stdSerializers.res; | ||
err && ( opts.serializers.err = opts.serializers.err || bunyan.stdSerializers.err); | ||
logger = bunyan.createLogger(opts); | ||
} | ||
var level = levelFn(status, err); | ||
logFn = logger[level] ? logger[level] : logger.info; | ||
logFn = childLogger[level] ? childLogger[level] : childLogger.info; | ||
@@ -127,5 +143,5 @@ ip = ip || req.ip || req.connection.remoteAddress || | ||
if (!json) { | ||
logFn.call(logger, format(meta)); | ||
logFn.call(childLogger, format(meta)); | ||
} else { | ||
logFn.call(logger, json, format(meta)); | ||
logFn.call(childLogger, json, format(meta)); | ||
} | ||
@@ -167,1 +183,9 @@ } | ||
} | ||
function defaultGenReqId(req) { | ||
var requestId = uuid.v4(); | ||
req.id = requestId; | ||
return requestId; | ||
} |
{ | ||
"name": "express-bunyan-logger", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "a bunyan logger middleware for express", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "make test" | ||
"lint": "./node_modules/.bin/jshint index.js test/*.js", | ||
"test": "npm run lint && ./node_modules/.bin/mocha -R spec" | ||
}, | ||
@@ -20,2 +21,3 @@ "repository": { | ||
"bunyan": "^1.0.0", | ||
"node-uuid": "^1.4.1", | ||
"useragent": "^2.0.9" | ||
@@ -40,7 +42,8 @@ }, | ||
"contributors": [ | ||
"Wei Gao <jky239@gmail.com>", | ||
"Paris Holley <mail@parisholley.com>", | ||
"Ognian Tschakalov <ognian.tschakalov@ogi-it.com>", | ||
"Simon Wade <simon.wade@gmail.com>", | ||
"Wei Gao <jky239@gmail.com>" | ||
"Simon Wade <simon.wade@gmail.com>" | ||
], | ||
"license": "BSD" | ||
} |
@@ -37,4 +37,13 @@ # Express-bunyan-logger | ||
}); | ||
And a child logger will be attached to each request object: | ||
```javascript | ||
app.use(require('express-bunyan-logger')(); | ||
app.use(function(req, res, next) { | ||
req.log.debug('this is debug in middleware'); | ||
next(); | ||
}); | ||
``` | ||
## Configuration | ||
@@ -71,2 +80,18 @@ | ||
### optiosn.genReqId | ||
By default, `express-bunyan-logger` will generate an unique id for each request, and a field 'req_id' will be added to child logger in `request` object. | ||
If you have already use other middleware/framework to generate request id, you can pass a function to retrieve it: | ||
```javascript | ||
// suppose connect-requestid middleware is already added. | ||
app.use(require('express-bunyan-logger')({ | ||
genReqId: function(req) { | ||
return req.id; | ||
} | ||
}); | ||
``` | ||
## License | ||
@@ -73,0 +98,0 @@ |
@@ -14,3 +14,3 @@ var express = require('express'); | ||
if(this.content) | ||
this.content = Buffer.concat([content, chunk]); | ||
this.content = Buffer.concat([this.content, chunk]); | ||
else | ||
@@ -73,2 +73,68 @@ this.content = chunk; | ||
it('test request id', function(done) { | ||
var app = express(); | ||
var output = st(); | ||
app.use(bunyanLogger({ | ||
stream: output | ||
})); | ||
app.use(function(req, res, next) { | ||
req.log.info('middleware'); | ||
next(); | ||
}); | ||
app.get('/', function(req, res) { | ||
res.send('GET /'); | ||
}); | ||
request(app) | ||
.get('/') | ||
.expect('GET /', function(err, res) { | ||
var lines = output.content.toString().split('\n'); | ||
assert.equal(lines.length, 3); | ||
assert.equal(lines[2], ''); | ||
var json = JSON.parse(lines[0]); | ||
assert.equal(json.name, 'express'); | ||
assert(json.req_id); | ||
var req_id = json.req_id; | ||
assert.equal(json.msg, 'middleware'); | ||
json = JSON.parse(lines[1]); | ||
assert.equal(json.url, '/'); | ||
assert(json.req_id); | ||
assert.equal(json.req_id, req_id); | ||
done(); | ||
}); | ||
}); | ||
it('test options.genReqId', function(done) { | ||
var app = express(); | ||
var output = st(); | ||
var id = 0; | ||
app.use(bunyanLogger({ | ||
stream: output, | ||
genReqId: function(req) { | ||
return id++; | ||
} | ||
})); | ||
app.get('/', function(req, res) { | ||
res.send('GET /'); | ||
}); | ||
request(app) | ||
.get('/') | ||
.expect('GET /', function(err, res) { | ||
var json = JSON.parse(output.content.toString()); | ||
assert.equal(json.name, 'express'); | ||
assert.equal(json.req_id, 0); | ||
done(); | ||
}); | ||
}); | ||
it('test excludes', function(done) { | ||
@@ -118,3 +184,2 @@ var app = express(); | ||
assert.equal(json.name, 'express'); | ||
console.log(json); | ||
assert(!json.url); | ||
@@ -146,3 +211,2 @@ assert(!json['status-code']); | ||
assert.equal(json.url, '/'); | ||
console.log(json); | ||
assert.equal(json['status-code'], 500); | ||
@@ -158,2 +222,3 @@ assert(json.res && json.req && json.err); | ||
var app = express(); | ||
var output = st(); | ||
@@ -164,3 +229,6 @@ app.get('/', function(req, res) { | ||
app.use(bunyanLogger.errorLogger()); | ||
app.use(bunyanLogger.errorLogger({ | ||
stream: output | ||
})); | ||
app.use(function (err, req, res, next) { | ||
@@ -167,0 +235,0 @@ middlewareCalled = true; |
17262
12
376
100
3
+ Addednode-uuid@^1.4.1
+ Addednode-uuid@1.4.8(transitive)