Skip to content
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

fix: twice customProps #288

Merged
merged 3 commits into from Aug 4, 2023
Merged

Conversation

youngkiu
Copy link
Contributor

@youngkiu youngkiu commented Jun 11, 2023

Hello!
I am the author of pino-web-hook.

I am using the NestJS framework, and I wanted to display the pinoHttp property, thinking that the main function of nestjs-pino is Middleware that connects pino-http.

    LoggerModule.forRoot({
      pinoHttp: {
        customProps: (req, res) => ({
          context: 'HTTP',
        }),
      },
    }),

However, I found the following bug, and also found out that there is an issue #216.
image

I tried the following to solve this problem.

  1. I tried to use onchild-function, but stopped because the instance given as a parameter was a logger instance that could be known outside of OnChildCallback.
+   opts.onChild = (instance) => {
+     // Execute call back code for each newly created child.
+   }
    const logger = wrapChild(opts, theStream)
  1. The second method is the current PR. Additional conditional statements can be written on one line, but for readability, I have declared local variables.

I debugged it with the following modified example.js file.

const logger = require('./')({
  customProps: (req, res) => ({
    context: 'HTTP'
  })
})

Thanks

@jmealo
Copy link
Contributor

jmealo commented Jul 21, 2023

@youngkiu I thought this was by design to be fast... when the JSON is parsed, most parsers I have tested will keep the last entry logged, so there shouldn't be any dupes appearing in your log viewer? [however, the raw log output locally before it gets shipped/processed, would show the duplicates, possibly]... I don't see a way of fixing this that isn't a performance regression, and for most folks, I don't think it matters. I would have assumed a pretty printer when running locally would have fixed this? I'm not using cloud functions, but if the logging was JSON native, and you disable pretty printing, and let Google format it for you, it shouldn't have duplicates (if that's an option).

I just read all the threads on this, it looks like breaking up the customProps function may make sense. It'd be interesting to test your fix versus a WeakSet from a performance standpoint. I'm having a hard time reasoning about how your fix works, but it didn't break any tests which is promising. 🤷

@youngkiu
Copy link
Contributor Author

@jmealo Thanks for your interest in my fix.

I understand that using pino-pretty will output customProps without duplication due to JSON parsing.
Especially in the production log, I think it's more important to save it raw than to look pretty. - We plan to write a program that can analyze the accumulated logs if necessary in the future.
I don't use a separate JSON parser, but I modified it because duplicate logs were inconvenient.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for opening a PR! Can you please add a unit test?

@jmealo
Copy link
Contributor

jmealo commented Jul 24, 2023

@youngkiu: If you need a hand with unit tests let me know. I think a failing test for the current implementation would be the easiest way to approach this.

@youngkiu
Copy link
Contributor Author

youngkiu commented Jul 24, 2023

@jmealo I'm still not sure how I was able to see logger[chindingsSym] in unit test. Can you help me write the test code?

@youngkiu
Copy link
Contributor Author

youngkiu commented Aug 2, 2023

@mcollina
In master branch, customProps is number 2,
From my fix, I added a test case to ensure that customProps is number 1.
Please review PR

@youngkiu youngkiu requested a review from mcollina August 2, 2023 15:43
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina mcollina requested a review from jsumners August 2, 2023 16:54
@mcollina mcollina merged commit bea64cc into pinojs:master Aug 4, 2023
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants