Coder Social home page Coder Social logo

bricka / superagent-bunyan Goto Github PK

View Code? Open in Web Editor NEW

This project forked from joaquimserafim/superagent-bunyan

0.0 3.0 0.0 30 KB

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

License: ISC License

JavaScript 100.00%

superagent-bunyan's Introduction

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)

superagent-bunyan's People

Contributors

joaquimserafim avatar jgalmeida avatar

Watchers

Alex Figl-Brick avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.