pino-http
pino-http copied to clipboard
Option to not create intermediate child logger
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?