express-bunyan-logger
Advanced tools
Comparing version 1.1.1 to 1.2.0
33
index.js
@@ -21,3 +21,4 @@ var bunyan = require('bunyan'), | ||
genReqId = defaultGenReqId, | ||
levelFn = defaultLevelFn; | ||
levelFn = defaultLevelFn, | ||
includesFn; | ||
@@ -50,3 +51,8 @@ if (opts.logger) { | ||
if (opts.includesFn) { | ||
includesFn = opts.includesFn; | ||
delete opts.includesFn; | ||
} | ||
if (opts.genReqId) { | ||
@@ -96,8 +102,5 @@ genReqId = typeof genReqId == 'function' ? opts.genReqId : defaultGenReqId; | ||
var level = levelFn(status, err); | ||
logFn = childLogger[level] ? childLogger[level] : childLogger.info; | ||
ip = ip || req.ip || req.connection.remoteAddress || | ||
(req.socket && req.socket.remoteAddress) || | ||
(req.socket.socket && req.socket.socket.remoteAddresss) || | ||
(req.socket.socket && req.socket.socket.remoteAddress) || | ||
'127.0.0.1'; | ||
@@ -127,2 +130,5 @@ | ||
var level = levelFn(status, err, meta); | ||
logFn = childLogger[level] ? childLogger[level] : childLogger.info; | ||
var json = meta; | ||
@@ -144,2 +150,12 @@ if (excludes) { | ||
if (includesFn) { | ||
var includes = includesFn(req, res); | ||
if (includes) { | ||
for (var p in includes) { | ||
json[p] = includes[p]; | ||
} | ||
} | ||
} | ||
if (!json) { | ||
@@ -155,8 +171,7 @@ logFn.call(childLogger, format(meta)); | ||
logging(true); | ||
} else { | ||
res.on('finish', logging); | ||
res.on('close', logging); | ||
} | ||
res.on('finish', logging); | ||
res.on('close', logging); | ||
next(err); | ||
@@ -163,0 +178,0 @@ }; |
{ | ||
"name": "express-bunyan-logger", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "a bunyan logger middleware for express", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -5,5 +5,9 @@ # Express-bunyan-logger | ||
[![Build Status](https://travis-ci.org/villadora/express-bunyan-logger.png?branch=master)](https://travis-ci.org/villadora/express-bunyan-logger) [![dependencies](https://david-dm.org/villadora/express-bunyan-logger.png)](https://david-dm.org/villadora/express-bunyan-logger) | ||
[![Build Status](https://travis-ci.org/villadora/express-bunyan-logger.svg?branch=master)](https://travis-ci.org/villadora/express-bunyan-logger) [![dependencies](https://david-dm.org/villadora/express-bunyan-logger.svg)](https://david-dm.org/villadora/express-bunyan-logger) | ||
## Note | ||
This year as work content change, I have no spare time to maintaining the node modules, if anyone want to take or keep maintaining, just contact me via jky239@gmail.com with Title contains: "Wanted: npm package xxxx". Thx. | ||
## Installation | ||
@@ -63,11 +67,30 @@ | ||
Function that translate statusCode into log level. | ||
Function that translate statusCode into log level. The `meta` argument is an object consisting of all the fields gathered by bunyan-express-logger, before exclusions are applied. | ||
``` | ||
function(status, err /* only will work in error logger */) { | ||
function(status, err /* only will work in error logger */, meta) { | ||
// return string of level | ||
return "info"; | ||
if (meta["response-time"] > 30000) { | ||
return "fatal"; | ||
} else { | ||
return "info"; | ||
} | ||
} | ||
``` | ||
### options.includesFn | ||
Function that is passed `req` and `res`, and returns an object whose properties will be added to the meta object passed to bunyan | ||
```javascript | ||
function(req, res) { | ||
if (req.user) { | ||
return { | ||
_id: req.user._id, | ||
name: req.user.name | ||
} | ||
} | ||
} | ||
``` | ||
### options.excludes | ||
@@ -77,2 +100,14 @@ | ||
### options.serializers | ||
An object of [bunyan serializers](https://github.com/trentm/node-bunyan#serializers). They are passed on to bunyan. | ||
The default serializers are defined as follows: | ||
``` | ||
{ | ||
req: bunyan.stdSerializers.req, | ||
res: bunyan.stdSerializers.res, | ||
err: bunyan.stdSerializers.err | ||
} | ||
``` | ||
### options.immediate | ||
@@ -82,3 +117,3 @@ | ||
### optiosn.genReqId | ||
### options.genReqId | ||
@@ -85,0 +120,0 @@ 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. |
@@ -242,4 +242,58 @@ var express = require('express'); | ||
}); | ||
it('test options.includesFn', function (done) { | ||
var app = express(); | ||
var output = st(); | ||
app.use(bunyanLogger({ | ||
stream: output, | ||
includesFn: function (req, res) { | ||
return { | ||
user: { | ||
name: 'Eric', | ||
_id: '546f80240a186fd6181472a9' | ||
} | ||
}; | ||
} | ||
})); | ||
app.get('/', function (req, res) { | ||
res.send('GET /'); | ||
}); | ||
request(app) | ||
.get('/') | ||
.expect('user property to be present in log', function (err, res) { | ||
var json = JSON.parse(output.content.toString()); | ||
assert(json.user); | ||
assert.equal(json.user.name, 'Eric'); | ||
done(); | ||
}); | ||
}); | ||
it('test options.levelFn', function (done) { | ||
var app = express(); | ||
var output = st(); | ||
app.use(bunyanLogger({ | ||
stream: output, | ||
levelFn: function (status, err, meta) { | ||
if (meta && meta['response-time'] !== undefined) { | ||
return 'fatal'; | ||
} | ||
} | ||
})); | ||
app.get('/', function (req, res) { | ||
res.send('GET /'); | ||
}); | ||
request(app) | ||
.get('/') | ||
.expect('error level fatal', function (err, res) { | ||
var json = JSON.parse(output.content.toString()); | ||
assert.equal(json.level, 60); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
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
19401
417
135
2