-
Notifications
You must be signed in to change notification settings - Fork 118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
customProps always logs duplicate keys #216
Comments
Can you include what you are trying to achieve? The snippet is not enough. |
Currently, any usage of Right now, as a user, I am forced to deal with duplicate keys if I want to use the |
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. |
Got same issue here, want to add customProps to the last log e.g |
I took a look at this over the weekend, and unfortunately I couldn't come up with a quick solution that doesn't cause unit tests to break. This one may require some time/care to fix correctly and maintain all current behavior. In order to pass the correct (final) res.statusCode to the customProps bindings and have the bindings in logs on |
I have the same issue. Couldn't we just remove the
This will be done on the The only question i have is, does the function What do you think ? |
Hello @mcollina 👋 Here is a reproduction repository for this issue. You'll see in the log output duplicate keys because customProps is called at the loggingMiddleware level and also the onResFinished level. To reproduce :
|
This comment was marked as duplicate.
This comment was marked as duplicate.
@rbadr can you simplify that repository by removing all traces of Nest? I'm not familiar with it and debugging something through all indirection is very hard. Could you reproduce just with pino-http? |
Yeah. I can reproduce in pino-http. Here an example: File: pino-config.ts import { Options } from 'pino-http';
import { IncomingMessage } from 'http';
export const pinoHttpConfig: Options = {
quietReqLogger: false,
autoLogging: true,
base: { hostname: os.hostname },
level: 'info',
formatters: {
level: (label: string) => {
return { level: label };
},
},
timestamp: () => `,"time":"${new Date(Date.now()).toISOString()}"`,
serializers: {
req: () => {},
},
customProps: function (req: IncomingMessage) {
// Dummy message
return { hola: 'hola' };
},
}; Then, with pino-http: File: logger.middleware.ts import pino from 'pino-http';
import { pinoHttpConfig } from './pino.config';
export function LoggerMiddleware() {
return pino(pinoHttpConfig);
} Injecting middleware in express app: app.use(LoggerMiddleware()); As you can see: "hola", appears twice. |
This is because
I think #197 had a bad bug we didn't see at the time. Lines 107 to 110 in 3361ced
Would you like to send a PR? |
@mcollina I don't see a way to build an artifact and test all changes in my local. No information in the README.md or ci.yml, please could you provide the steps to test in my local and see the expected result? I am following these steps (https://dev.to/scooperdev/use-npm-pack-to-test-your-packages-locally-486e) to link the dependency and test. |
Workaround: use a |
To make the changes, run |
https://jrfom.com/posts/2017/03/08/a-primer-on-contributing-to-projects-with-git/ if you need another resource. |
@mcollina if I remember correctly, you can't just remove one of the bindings - it causes a lot of other things to break and then you don't have the custom bindings in normal calls to @mpartel had an interesting idea of using a |
To be clear, I'm just suggesting |
@mpartel thanks for clarifying. I'm not well versed in the application of |
This seems to work: const requestsSeen = new WeakSet();
// ...
customProps: (req) => {
if (requestsSeen.has(req)) {
return undefined;
}
requestsSeen.add(req);
return { stuff };
} |
We should really split |
Hello, I just bumped into this and sadly don't have the time to do a PR but I have a case where the first call will happen with a Request object that has not yet fully gone through the pipeline. I wanted to log the matched expressjs route and the current user user id, and in order to do that I had to do something like: customProps(req) {
if (!req['SEEN-THIS-ONE']) {
req['SEEN-THIS-ONE'] = true;
} else if (req['SEEN-THIS-ONE']) {
return {
route: req.route.path,
userId: req.user.id,
};
}
} If I used the values from the first call they would always be The stack I have is express, nestjs, nestjs-pino. |
This comment was marked as off-topic.
This comment was marked as off-topic.
is this issue fixed? If yes then in which version. I am trying to log 'x-request-id' in the outermost json and for response logging it is appending twice. |
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. |
I tried to resolve it at #288. |
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? |
@cat-pierrecharles |
Awesome, thank you @youngkiu |
Has the fix in the latest release resolved this problem for everyone? I am still seeing it on the latest version unfortunately. |
@youngkiu's fix in August solved it for me, I'm currently using 8.4.0 so not sure if the latest release have introduced that issue or not |
@rickihastings @cat-pierrecharles
This is the log generated from my repository code
|
It seems that I can't recreate this problem locally but it's an issue when deployed to GCP Cloud Run. I'll do some further investigation to see if I can replicate it outside of GCP. |
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. import express from 'express';
import pino from 'pino-http';
const app = express();
const logger = pino({
customProps: (req, res) => {
if (req.isLogged) return {};
req.isLogged = true;
return {
auth: decodeToken(req.headers.authorization),
}
},
});
declare module 'http' {
interface IncomingMessage {
isLogged: boolean;
}
}
|
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 This worked for me to log those props only when the request is finished by checking customProps: function (_req, res) {
if (!res.writableEnded) {
return {};
}
return {
context: getRequestContext()
};
}, |
@henripoikonen's solution works for me. Here is more background on why |
According to this: pinojs/pino#625 @mcollina what do you think? |
PRs are welcome. |
customProps always logs duplicate keys.
I am working with google cloud logging and it doesn't play nicely with the duplicate keys:
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 #197
The text was updated successfully, but these errors were encountered: