superagent-bunyan
DefinitelyTyped icon, indicating that this package has TypeScript declarations provided by the separate @types/superagent-bunyan package

6.0.0 • Public • Published

superagent-bunyan

a plugin for superagent that uses bunyan to log the request and responses


Build StatusCode Coverage 100%ISC LicenseNodeJS

JavaScript Style Guide

api

const superagentLogger = require('superagent-bunyan')

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
  • to capture the X-Request-ID should use this plugin after setting the X-Request-ID header
  • for more options to configure bunyan.createLogger or log.child check with bunyan doc
  • will use log.error with http errors (status codes 4xx and 5xx) and socket errors but for the http errors res will have the statusCode

example

const request = require('superagent')
const bunyan  = require('bunyan')
 
const superagentLogger = require('superagent-bunyan')
 
const logger = bunyan.createLogger({name: 'my_log'})
 
logger.info('Hey!')
 
request
  .get('http://localhost:3000')
  .use(superagentLogger(logger)))
  .end((err, res) => {})
 
//
// 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": 27.22063, // 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
  .get('http://localhost:3000')
  .set('X-Request-ID', uuid)
  .use(superagentLogger(logger)))
  .end((err, res) => {})

ISC License (ISC)

Dependents (0)

Package Sidebar

Install

npm i superagent-bunyan

Weekly Downloads

27

Version

6.0.0

License

ISC

Unpacked Size

8.5 kB

Total Files

4

Last publish

Collaborators

  • jgalmeida
  • jgantunes
  • quim