Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/firstandthird/hapi-log-response
Hapi plugin to log responses based on status code
https://github.com/firstandthird/hapi-log-response
hapi-plugin hapi-v17
Last synced: 3 days ago
JSON representation
Hapi plugin to log responses based on status code
- Host: GitHub
- URL: https://github.com/firstandthird/hapi-log-response
- Owner: firstandthird
- License: mit
- Created: 2014-10-11T22:42:49.000Z (over 10 years ago)
- Default Branch: main
- Last Pushed: 2023-01-07T22:22:38.000Z (about 2 years ago)
- Last Synced: 2024-08-10T09:59:42.918Z (6 months ago)
- Topics: hapi-plugin, hapi-v17
- Language: JavaScript
- Size: 784 KB
- Stars: 2
- Watchers: 6
- Forks: 1
- Open Issues: 15
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
hapi-log-response
A [hapi](https://hapi.dev/) plugin that uses the [server.log()](https://hapi.dev/api/?v=20.1.0#-serverlogtags-data-timestamp) 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
```sh
npm install hapi-log-response
```_or_
```sh
yarn add hapi-log-response
```## Usage
Register just like any other hapi plugin:
```javascript
await server.register({
plugin: require('hapi-log-response'),
options: {}
});
``````js
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](https://firstandthird.com) Project_