pino-http: customProps always logs duplicate keys
customProps always logs duplicate keys.
I am working with google cloud logging and it doesn’t play nicely with the duplicate keys:
customProps(req) {
// adds a custom object to all http logs that stackdriver
// uses to make logs more informative and visible at a glance
// see: https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#httprequest
// for the details of the structured log messages
return {
httpRequest: {
requestMethod: req.method,
requestUrl: req.url,
userAgent: req.headers['user-agent'],
remoteIp:
req.headers['x-forwarded-for']?.split(',').shift() ||
req.socket?.remoteAddress,
},
};
},
For instance:
custom props cause the following log:
"{ "httpRequest": {"method":"GET"},"httpRequest": {"method":"GET"}}"
ends up in stackdriver/google cloud logging looking like:
{httpRequest: { method: 'GETGET' }}
related to https://github.com/pinojs/pino-http/pull/197
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 11
- Comments: 35 (11 by maintainers)
I stumbled into the same issue but the workaround proposed above didn’t work for me since I want to log some custom context that is not yet available when the
customProps
function is first called.This worked for me to log those props only when the request is finished by checking
res.writableEnded
property:This seems to work:
This is not fixed and a PR would be greatly appreciated, I currently have no time to work on this and I’m not really using it anywhere.
Your usecase is a bit far from mine.
Would you like to send a Pull Request to address this issue? Remember to add unit tests.
This issue hasn’t been fixed in v8.5.0.
The workaround made by @ilari-makimattila looks good to me, and those who might be using in Typescript can try this.
@cat-pierrecharles I also wrote the unit test requested by the maintainer in my fix. Please wait a moment for it to be merged into the master branch.
Hey everyone, I think this issue is still happening. I’m using customProps and like everyone else it is still duplicating the values. Could anything be done to fix this?
Yeah. I can reproduce in pino-http.
Here an example:
File: pino-config.ts
Then, with pino-http:
File: logger.middleware.ts
Injecting middleware in express app:
Output:
As you can see: “hola”, appears twice.
I have the same issue.
Couldn’t we just remove the
customPropBindings
on theloggingMiddleware
This will be done on the
onResFinished
function. I tested it quickly and it work.The only question i have is, does the function
onResFinished
called on 100% use cases ? Like on network failure, timeout …What do you think ?