GithubHelp home page GithubHelp logo

isabella232 / hapi-log-response Goto Github PK

View Code? Open in Web Editor NEW

This project forked from firstandthird/hapi-log-response

0.0 0.0 0.0 528 KB

Hapi plugin to log responses based on status code

License: MIT License

JavaScript 99.57% HTML 0.43%

hapi-log-response's Introduction

hapi-log-response

Test Status Lint Status NPM

A hapi plugin that uses the server.log() function to log requests/responses based on their statusCode. By default it will only log redirects (HTTP 301/302) and errors (HTTP 400-500), but you can customize it to include/exclude any status code you want, or just have it log all requests regardless of code.

Installation

npm install hapi-log-response

or

yarn add hapi-log-response

Usage

Register just like any other hapi plugin:

await server.register({
  plugin: require('hapi-log-response'),
  options: {}
});
server.route({
  method: 'GET',
  path: '/error',
  handler(request, h) {
    throw boom.badRequest('HELLO WORLD');
  }
});

Calling that route will result in something like the following log:

[ 'detailed-response', 'user-error', 'bad-request' ]
{
 browser: 'Other 0.0.0 / Other 0.0.0',
 userAgent: 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0',
 isBot: false,
 ip: '127.0.0.1',
 method: 'get',
 path: '/error',
 query: {},
 message: '/error: HELLO WORLD',
 statusCode: 400,
 error: {
   message: 'HELLO WORLD',
   stack: 'Error: HELLO WORLD\n
      at handler (/script.js:26:12)
      ....'

Default tags

hapi-log-response will add the following tags to the log depending on the HTTP status of the request:

  • 301 'redirect'
  • 302 'redirect'
  • 400 'bad-request'
  • 401 'unauthorized'
  • 500 'internal-server'
  • 503 'service-unavailable'
  • 504 'client-timeout'

Additionally hapi-log-response will add the 'detailed-response' tag to every log it enters, unless you requested something different using the tag option (see below).

Options

You can configure hapi-log-response by configuring the following options when you register the plugin:

  • requests

    By default hapi-log-responses only responds when the request results in either a redirect (HTTP 301 or 302) or an error (anything between HTTP 400 through HTTP 500). Set this option to true to always log all requests regardless of HTTP status. Setting this will override the excludeStatus option.

  • requiredTags

    An array of tags to match, tags that don't match this will not be logged.

  • excludeStatus

    An array of HTTP statuses you want to ignore. Responses that match any of these statusCodes will not be logged. However the requests option logs everything and will override this if set to true.

  • includeId

    Will include the hapi-assigned ID for the request, default is false.

  • ignoreUnauthorizedTry

    By default hapi-log-response will log any time a client tries to access a protected route. This can get really obnoxious when your server is being probed by the various crawlers and bots running around the Internet, so set this to 'true' to ignore those failed attempts.

  • includeEventTags

    By default event tags like handler are not logged, set this option to true to see them.

  • requestPayload

    For POST routes, you can opt to also log the incoming request payload. Default is false.

  • requestHeaders

    Setting this option to true will cause request headers to be included in the logs. By default they are not included.

  • tags

    An array containing zero or more strings that will be added to the list of tags accompanying the log. If not specified, the default is ['detailed-response'].


A First + Third Project

hapi-log-response's People

Contributors

dawnerd avatar ecwillis avatar jgallen23 avatar orthagonal 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.