Skip to content

firstandthird/hapi-log-response

Repository files navigation

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