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

Option to not create intermediate child logger #266

Open
davesteinberg opened this issue Jan 5, 2023 · 2 comments
Open

Option to not create intermediate child logger #266

davesteinberg opened this issue Jan 5, 2023 · 2 comments

Comments

@davesteinberg
Copy link

When the logger option is specified, pino-http doesn't actually use that logger as the parent for each created request logger, it creates an intermediate child logger (which is then used as the parent for each created request logger). Although it's documented, I was very surprised when I realized this is how it works.

When I'm writing unit tests to cover error conditions, and I expect them to log the error, I like to set logger.level = silent to avoid messing up my test output with expected error messages. I've done this previously with other logging libraries, and the change applied to request loggers as well, because they were created at the time the request is handled, which is after the level on the parent changes. Because of pino-http's intermediate logger, this approach doesn't work. Instead, I need to get the intermediate parent logger from the logger property of the returned middleware function, and pass it around so that it's accessible in my test cases where I need to access it. This complicates my application code, forcing me to deviate from usual Express patterns and adding logging concerns where you might not expect them. Also, if a test touches on code that uses both the global default logger and a request logger, then I have to change the level on both.

So, I wonder if it would be possible and desirable to have an option that doesn't create the intermediate logger, stores whatever customization would have been on it off to the side, uses the provided logger as the parent, and applies that customization when the request logger is created?

@mcollina
Copy link
Member

mcollina commented Jan 5, 2023

Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests.

@jsumners
Copy link
Member

jsumners commented Jan 6, 2023

Instead of using level = silent, you should supply a stream that collects the logs and then inspect that collection. See pinojs/pino#1274 (comment).

By providing a stream, at least according to the documentation, you don't need to concern yourself with passing around any references to any loggers created by this module. They will be writing to the destination you control.

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

No branches or pull requests

3 participants