How to make hapi nice to ignore a specific route

I would like to have a good ignore log for a specific route, as it checks the health check route through amazon every few seconds - it just adds noise to our logs.

Is it possible to configure a specific route configuration for a good plugin so that it simply ignores one specific route?

+5
source share
3 answers

It can be done. Here is the documentation https://github.com/hapijs/good/blob/master/API.md#stream-transforms-using-plugin-configs

Hapi Route Configuration

var routeConfig = { plugins: { good: { suppressResponseEvent: true } } }; server.route({ method: '*', path: '/suscribe/{path*}', handler: function(req, rep){ rep("OK!"); }, config: routeConfig }); 

Create a custom filter for the good . You will need to create a new npm package. In this example, we will call it a good filter. Follow the architecture https://github.com/hapijs/good-squeeze/ The main file (index.js) contains:

 'use strict'; module.exports = { Filter: require("./filter.js") }; 

This package should be available when downloading good . The following code goes into filter.js in a good filter package.

 'use strict'; const Stream = require('stream'); class Filter extends Stream.Transform { constructor(options) { options = Object.assign({}, options, { objectMode: true }); super(options); } _transform(data, enc, next) { if (data.event === 'response' && data.config.suppressResponseEvent === true) { return next(); } return next(null, data); } } module.exports = Filter; 

Finally, add a filter to a good configuration.

 const options = { ops: { interval: 1000 }, reporters: { myConsoleReporter: [{ module: 'good-squeeze', name: 'Squeeze', args: [{ log: '*', response: '*' }] }, { module: 'good-filter', name: 'Filter', args: [{ log: '*', response: '*' }] }, { module: 'good-console' }, 'stdout'] } }; 
+2
source

Here's what works, at least with hapi 16.1, good 7.1 and good-squeeze 5.0. The idea is to mark the health route log, and then exclude this route with good-squeeze .

Configuring good / good-squeeze to exclude " health ":

 server.register({ register: Good, options: { reporters: { console: [ { module: 'good-squeeze', name: 'Squeeze', args: [ { // keep health checks from appearing in logs response: { exclude: 'health' }, log: '*', }, ], }, { module: 'good-console', }, 'stdout', ], }, }, }); 

And then mark your route:

 server.route({ method: 'GET', path: '/admin/ok', handler: (request, reply) => reply('ok'), config: { tags: ['health'], }, }); 
+1
source

I assume that you are looking at ignoring the response log, which is not configurable to my knowledge. For other logging, you can filter events using tags.

Following:

  var someData = {foo:'bar'}; request.log('myTag', someData); 

generates the following output:

 150928/224019.555, [request,myTag], data: {"foo":"bar"} 

The following good options might select this:

 var loggingOpts = { reporters: [{ reporter: require('good-console'), events: { request: 'myTag' } }] }; 
0
source

Source: https://habr.com/ru/post/1232479/


All Articles