Skip to content

Commit

Permalink
Merge pull request #9 from joaquimserafim/v5
Browse files Browse the repository at this point in the history
v5
  • Loading branch information
joaquimserafim authored May 5, 2017
2 parents 4f2211e + 507d566 commit 61f52e7
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 31 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ a plugin for superagent that uses bunyan to log the request and responses
`const superagentLogger = require('superagent-bunyan')`

```js
superagentLogger(bunyan_logger[, requestId])
superagentLogger(bunyan_logger[, requestId, extra])
```

* **bunyan logger** should be a bunyan createLogger or child object
* **requestId** uuid, by default will try to pick up from the headers injected in superagent, or can be set by using a module/function to generate the requestId, by default will use an internal id generator
* **extra** an object that you can use to add extra info int the log entry

**some notes:**
* should use the plugin after the definiton of the http method to use
Expand Down
40 changes: 21 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@ max-len: ["error", 80]
const getPropValue = require('get-property-value')
const objectSize = require('object.size')
const url = require('url')
const isObject = require('is.object')

module.exports = logger

function logger (bunyan, requestId) {
function logger (bunyan, requestId, extra) {

if (isObject(requestId)) {
extra = requestId
requestId = null
}

return runLogger

Expand All @@ -21,22 +27,22 @@ function logger (bunyan, requestId) {
let endTime = 0
const startTime = process.hrtime()

const service = req.url
const method = req.method

req.id = requestId || getPropValue(req.header, 'X-Request-ID') || id()

const log = bunyan.child(
{
origin: 'superagent',
req_id: req.id,
serializers: {
err: bunyan.constructor.stdSerializers.err,
req: reqSerializer,
res: resSerializer
}
}
)
const log = bunyan
.child(Object
.assign({
origin: 'superagent',
req_id: req.id,
serializers: {
err: bunyan.constructor.stdSerializers.err,
req: reqSerializer,
res: resSerializer
}
},
isObject(extra) ? extra : {}
)
)

log.info({req: req}, 'start of the request')

Expand All @@ -57,8 +63,6 @@ function logger (bunyan, requestId) {
log.error(
{
res: appendRes,
service: service,
method: method,
err: err,
duration: endTime[0] * 1e3 + endTime[1] * 1e-6
},
Expand All @@ -75,8 +79,6 @@ function logger (bunyan, requestId) {
log.info(
{
res: res,
service: service,
method: method,
duration: endTime[0] * 1e3 + endTime[1] * 1e-6
},
'end of the request'
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "superagent-bunyan",
"version": "4.3.0",
"version": "5.0.0",
"description": "a plugin for superagent that uses bunyan to log the request s and responses",
"main": "index.js",
"files": [
Expand Down Expand Up @@ -41,6 +41,7 @@
},
"dependencies": {
"get-property-value": "^2.0.0",
"is.object": "^1.0.0",
"object.size": "^1.0.0"
},
"engines": {
Expand Down
63 changes: 53 additions & 10 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ describe('superagent-bunyan', () => {
expect(log2.res).to.be.an('object')
expect(log2.res.statusCode).to.be.equal(200)
expect(log2.res.headers).to.be.an('object')
expect(log2.method).to.be.equal('GET')
done()
})
})
Expand All @@ -210,7 +209,6 @@ describe('superagent-bunyan', () => {
expect(log2.res).to.be.an('object')
expect(log2.res.statusCode).to.be.equal(200)
expect(log2.res.headers).to.be.an('object')
expect(log2.method).to.be.equal('GET')
done()
})
})
Expand All @@ -236,7 +234,6 @@ describe('superagent-bunyan', () => {
expect(log2.res).to.be.an('object')
expect(log2.res.statusCode).to.be.equal(200)
expect(log2.res.headers).to.be.an('object')
expect(log2.method).to.be.equal('POST')
done()
})
})
Expand All @@ -259,7 +256,6 @@ describe('superagent-bunyan', () => {
expect(log2.res.statusCode).to.be.equal(200)
expect(log2.res.body).to.be.deep.equal({msg: 'Hello World!'})
expect(log2.res.headers).to.be.an('object')
expect(log2.method).to.be.equal('GET')
done()
})
})
Expand All @@ -282,7 +278,6 @@ describe('superagent-bunyan', () => {
expect(log2.res).to.be.an('object')
expect(log2.res.statusCode).to.be.equal(200)
expect(log2.res.headers).to.be.an('object')
expect(log2.method).to.be.equal('GET')
done()
})
})
Expand All @@ -307,7 +302,59 @@ describe('superagent-bunyan', () => {
expect(log2.res).to.be.an('object')
expect(log2.res.statusCode).to.be.equal(200)
expect(log2.res.headers).to.be.an('object')
expect(log2.method).to.be.equal('GET')
done()
})
})
})

it('using `extra` to pass extra info in the log entry', (done) => {
request
.get('http://localhost:3000/someuuidbutdontsend')
.use(
superagentLogger(
logger,
{extra: 'But then again, who does?'}
)
)
.end((err, res) => {
expect(err).to.be.a('null')
expect(res).to.be.an('object')

testLogRecords(getRecords(), (log1, log2) => {
expect(log1.req.qs).to.be.an('undefined')
expect(log1.extra).to.be.equal('But then again, who does?')
expect(log2.err).to.not.exist
expect(log2.res).to.be.an('object')
expect(log2.res.statusCode).to.be.equal(200)
expect(log2.res.headers).to.be.an('object')
expect(log2.extra).to.be.equal('But then again, who does?')
done()
})
})
})

it('passing the request id and using `extra` arg', (done) => {
request
.get('http://localhost:3000/someuuidbutdontsend')
.use(
superagentLogger(
logger,
1234,
{extra: 'But then again, who does?'}
)
)
.end((err, res) => {
expect(err).to.be.a('null')
expect(res).to.be.an('object')

testLogRecords(getRecords(), (log1, log2) => {
expect(log1.req.qs).to.be.an('undefined')
expect(log1.extra).to.be.equal('But then again, who does?')
expect(log2.err).to.not.exist
expect(log2.res).to.be.an('object')
expect(log2.res.statusCode).to.be.equal(200)
expect(log2.res.headers).to.be.an('object')
expect(log2.extra).to.be.equal('But then again, who does?')
done()
})
})
Expand All @@ -327,7 +374,6 @@ describe('superagent-bunyan', () => {
expect(log2.res.statusCode).to.be.equal(404)
expect(log2.res.headers).to.be.an('object')
expect(log2.err.name).to.be.equal('Error')
expect(log2.method).to.be.equal('GET')
done()
})
})
Expand All @@ -345,7 +391,6 @@ describe('superagent-bunyan', () => {
expect(log2.res).to.be.an('object')
expect(log2.err).to.be.an('object')
expect(log2.err.name).to.be.equal('Error')
expect(log2.method).to.be.equal('GET')
done()
})
})
Expand Down Expand Up @@ -382,8 +427,6 @@ function testLogRecords (records, runExtraExpections) {
expect(record.req.headers).to.be.an('object')
} else { // the response
expect(record.msg).to.be.equal('end of the request')
expect(record.service).to.be.a('string')
expect(record.method).to.be.a('string')
expect(record.duration).to.be.a('number')
}
}
Expand Down

0 comments on commit 61f52e7

Please sign in to comment.