superagent-bunyan
a plugin for superagent that uses bunyan to log the request and responses
api
const superagentLogger = require('superagent-bunyan')
- 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
- to capture the
X-Request-ID
should use this plugin after setting theX-Request-ID
header - for more options to configure
bunyan.createLogger
orlog.child
check with bunyan doc - will use
log.error
with http errors (status codes 4xx and 5xx) and socket errors but for the http errorsres
will have the statusCode
example
const request = const bunyan = const superagentLogger = const logger = bunyan logger request ) //// should print 2 log entries// 1 - "msg":"start of the request"// 2 - "msg":"end of the request", this will print the statusCode and the body// "name": "test" // LOG NAME "hostname": "local" "pid": 54073 "origin": "superagent" // LOG ORIGIN "req_id": "ix6btz2q--wyq997" // REQUEST ID "level": 30 "req": // REQUEST "method": "GET" "url": "http://localhost:3000" "headers": "user-agent": "node-superagent/3.3.1" "msg": "start of the request" "time": "2016-12-26T16:57:22.132Z" "v": 0 "name": "test" // LOG NAME "hostname": "local" "pid": 54073 "origin": "superagent"// LOG ORIGIN "req_id": "ix6btz2q--wyq997" // REQUEST ID "level": 30 "res": // RESPONSE STATUS AND PAYLOAD "statusCode": 200 "body": "Hello World!" "duration": 2722063 // REQUETS DURATION "msg": "end of the request" "time": "2016-12-26T16:57:22.159Z" "v": 0 //// setting the X-Request-ID with superagent// and superagent-bunyan will use to set the req.id// request )