Can't send log from Hapi-Pino to Elasticsearch - node.js

I'm building server for a website use hapi and hapi-pino for logging.
I want to analyze log (about status code, route, timestamp) for some business purpose. I use elasticsearch and kibana to do it
Between hapi-pino and Elasticsearch, I try to use pino-elasticsearch to send log. However, it didn't work, elasticsearch didn't get anything
This is my code for registering hapi-pino:
const streamToElastic = PinoElasticsearch({
index: 'api',
type: 'log',
consistency: 'one',
node: 'http://elastic:changeme#localhost:9200',
'es-version': 6,
'bulk-size': 200,
ecs: true
});
await server.register({
plugin: HapiPino,
options: {
logPayload: true,
prettyPrint: process.env.NODE_ENV !== 'production',
redact: {
paths: ['req.headers', 'payload.user.password', 'payload.file'],
remove: true
},
stream: streamToElastic
}
});
Thanks for any helps and sorry about my poor English!

Related

How to use express-winston and winston-mongodb together?

I'm using express-winston and winston-mongodb to log express requests to mongodb.
express-winston config:
expressWinston.logger({
meta: true,
//...other unrelated config
})
winston-mongodb config:
new MongoDB({
//...other unrelated config
})
Logging to mongodb works, but meta field is null. Logging to file/console works perfectly.
The mongo plugin doesn't recognize the express plugin's log format.
How can I get them to play nice?
When you look at the express-winston and winston-mongodb codes, you can easily see the difference.
winston-mongodb writes the value that matches the metaKey field to the specified collection.
Therefore, if you define it as follows, the meta field will not be null.
...
transports: [
new winston.transports.Console({
format: winston.format.json({
space: 2
})
}),
new winston.transports.MongoDB({
db: config.db.mongooseURI,
options: config.db.options,
collection:'logs',
capped:true,
metaKey:'meta'
}),
],
meta: true,
...
This is what finally worked for me: v6.12.1 Noticed later that while this works for db logging, the meta is empty for the file transport. Not sure why.
const winston = require('winston');
module.exports = function(err, req, res, next) {
winston.error(err.message, {metadata: { prop: err } });
res.status(500).send('Something failed.');
};
If you want to log to File and MongoDB then:
winston.add(
new winston.transports.MongoDB({
db: process.env.CONNECTIONSTRING,
options: { useUnifiedTopology: true },
metaKey: 'meta'
})
)
module.exports = function (err, req, res, next) {
// Log the exception
winston.error({message: err.message, level: err.level, stack: err.stack, meta: err})
res.status(500).send("Something failed..Cannot connect to Server");
};

How to disable throttling in Lighthouse programmaticaly?

As per question title, how can I disable any throttling when using Lighthouse programmatically in NodeJS?
For v3 it's not working like this but as described here
https://developers.google.com/web/tools/lighthouse/v3/migration
e.g. like:
let opts = {
...
throttlingMethod: 'provided',
throttling: {
throughputKbps: 8000,
downloadThroughputKbps: 8000,
uploadThroughputKbps: 2000
}
};
this should help :(tested on lighthouse 2.9.4 )
lhr = await lighthouse(url, {
port: (new URL(browser.wsEndpoint())).port,
output: 'json',
logLevel: 'info',
disableDeviceEmulation: true,
disableCpuThrottling: true,
disableNetworkThrottling: true
});

Accessing Loopback config data from middleware

Say we are in Loopback middleware, such as
app.use(function(req,res,next){
// I am not sure what key to use here in the getter...
const config = app.get('?');
});
I want to access the overall config that Loopback is using.
I tried:
const config = app.get('config');
const config = app.get('env');
const config = app.get('datasources');
nothing gives me quite what I want.
Interestingly, this gives me:
console.log(app.get('restApiRoot'));
=> '/api'
so that's a clue to what's going on, but I want to get the parent object(s) for the above data.
how can we access the configuration that Loopback has loaded. The configuration of course varies by environment variables etc.
I want to log what datasources.x.js file was loaded and what config.x.js file was loaded, and any other server configuration info I can capture.
Having a lot of trouble figuring out how to do this.
This seems to be the same question I have:
https://github.com/strongloop/loopback/issues/1526
but they point me to the void that is Google Groups and I searched through there and couldn't find what the answer to this question.
This behavior is actually inherited from Express.
The entire config is stored in the app.settings object, with app.get(key) and app.set(key,value) just acting as getter/setter.
Doing console.log(app.settings); (in server/server.js for instance) it on a fresh loopback install returns the following:
{ 'x-powered-by': true,
etag: 'weak',
'etag fn': [Function: wetag],
env: 'development',
'query parser': 'extended',
'query parser fn': [Function: parseExtendedQueryString],
'subdomain offset': 2,
'trust proxy': false,
'trust proxy fn': [Function: trustNone],
view: [Function: View],
views: 'C:\\Users\\*******\\Documents\\GitHub\\lbtest\\views',
'jsonp callback name': 'callback',
host: '0.0.0.0',
port: 3000,
restApiRoot: '/api',
remoting:
{ context: { enableHttpContext: false },
rest: { normalizeHttpPath: false, xml: false },
json: { strict: false, limit: '100kb' },
urlencoded: { extended: true, limit: '100kb' },
cors: false,
errorHandler: { disableStackTrace: false } },
legacyExplorer: false,
'loopback-component-explorer': { mountPath: '/explorer' },
url: 'http://localhost:3000/' }

Disable Hapi logs on test environment

I'm using Hapi with Good on a small project and want to disable some logs while testing (process.env.NODE_ENV == 'test').
This is my Good configuration:
reporters: {
console: [{
module: 'good-console',
}, 'stdout']
}
And this is the test output:
➜ npm test
Index Route
✓ is ok
160622/214344.247, [response] http://paulodiovani-ideapad:3001: get / {} 200 (27ms)
How can I disable all logs, except error?
Can you post your good configuration? you probably want something like:
reporters: {
console: [
{
module: 'good-squeeze',
name: 'Squeeze',
args: [{error: '*'}]
},
{
module: 'good-console'
},
'stderr']
}

Hapi good logging depends on the enviroment

Is there a way to enable / disable
depends on the enviroment in good ?
For instance I'd like to run this
only when NODE_ENV is equal to develepement
var goodOptions = {
opsInterval: 1000,
reporters: [{
reporter: require('good-console'),
events: {log: '*', response: '*' , error: '*' , request: '*'}
}]
};
server.register({
register: require('good'),
options: goodOptions
},
function(err) {
if (err) {
throw err;
}
}
);
no ternary operator :)
You could just add:
if (process.env.NODE_ENV !== 'development') {
goodOptions.reporters = [];
}
If you start getting into complex configuration then you should take a look at hapijs/confidence which is a really powerful configuration tool. It would be overkill just for this though.

Resources