Closed indreek closed 2 years ago
'use strict'
const Hapi = require('@hapi/hapi')
async function start () {
// Create a server with a host and port
const server = Hapi.server({
// host: 0.0.0.0,
port: 3000
})
// Add the route
server.route({
method: 'GET',
path: '/',
handler: async function (request, h) {
return 'hello world'
}
})
await server.register({
plugin: require('hapi-pino'),
options: {}
})
// also as a decorated API
server.logger().info('another way for accessing it')
// and through Hapi standard logging system
server.log(['subsystem'], 'third way for accessing it')
await server.start()
return server
}
start().catch((err) => {
console.log(err)
process.exit(1)
})
Probably it comes from Pino https://getpino.io/#/docs/child-loggers?id=duplicate-keys-caveat
Hi
It was still bothering me.
const getChildBindings = options.getChildBindings ? options.getChildBindings : (request) => ({ req: request })
//....
const childBindings = getChildBindings(request)
request.logger = logger.child(childBindings) // You are adding child req: here,
// Although you are
// 1) adding it here when logRequestStart === true or
if (logRequestStart) {
request.logger.info({
req: request
}, 'request start')
}
tryAddEvent(server, options, 'on', 'response', function (request) {
//...
request.logger.info(
{
payload: options.logPayload ? request.payload : undefined,
tags: options.logRouteTags ? request.route.settings.tags : undefined,
// note: pino doesnt support unsetting a key, so this next line
// has the effect of setting it or "leaving it as it was" if it was already added via child bindings
req: logRequestStart ? undefined : request, // 2) adding it here when logRequestStart === false
res: request.raw.res,
responseTime: (info.completed !== undefined ? info.completed : info.responded) - info.received
},
'request completed'
)
quick fix for this in my opinion is to change options:
getChildBindings: () => ({})
We are seeing the same error as well.
@indreek, have you been able to identify the root cause?
Well, looks like the link right above says it all.
Thanks for sharing this :)
I'm using version 6.5.0
and I'm seeing duplicate values in the req
object in my logs:
x-appengine-country: "GBGB"
x-appengine-default-namespace: "gmail.comgmail.com"
x-appengine-https: "onon"
x-appengine-region: "engeng"
Hello it seems to be here and req: shouldLogRequestStart(request) || options.serializers.req ? undefined : request
does the trick
Setting getChildBindings: () => ({})
solves the issue for response logs but also gets rid of request data for request.log
logs (being done here https://github.com/pinojs/hapi-pino/blob/master/index.js#L140).
Not sure what the right solution is to get both 🤔
getChildBindings: () => ({})
gets rid of doubles in response logsgetChildBindings: (request) => ({ req: request})
adds the request info of request.log
but causes doubles in response logsevent
here https://github.com/pinojs/hapi-pino/blob/master/index.js#L140 which would allow the implementer to add the request only when event is passed?I'm using version
6.5.0
and I'm seeing duplicate values in thereq
object in my logs:x-appengine-country: "GBGB" x-appengine-default-namespace: "gmail.comgmail.com" x-appengine-https: "onon" x-appengine-region: "engeng"
I'm actually seeing the same thing. Due to the double req props, Google Cloud logging seems to resolve this by adding the value twice 😅
I think I've found a way to better handle this. Please take a look at https://github.com/pinojs/hapi-pino/pull/157
Any ideas why im getting req twice in logged json?