You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
The text was updated successfully, but these errors were encountered:
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.
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 thelogger
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?
The text was updated successfully, but these errors were encountered: