pinojs / pino-http

🌲 high-speed HTTP logger for Node.js
MIT License
527 stars 117 forks source link

Log req only on 1st log from the request #324

Closed palbrecht1 closed 7 months ago

palbrecht1 commented 7 months ago

I use pino-http in a NestJS project and my log messages look like this:

-> GET /my-endpoint
my log 1
my log 2
<- 200 GET /my-endpoint

Pino HTTP is what generates the first and last line and Pino is used for the inbetween (my log 1, my log 2).

My issue is that all my logs have the req.headers (and in my case a sanitized version of the req.body). Because there could be hundreds of log lines by Pino between the moment the request is received and the moment I send a response, the log output ends up annoyingly huge.

I'm already using a custom serializer to append req.body, but what I'm looking for is to only show req.headers (and req.body) in the first log for the request, not any of the subsequent ones. If I need to look what headers / body were sent in "my log 1", I'll just scroll up to read it from "-> GET /my-endpoint".

mcollina commented 7 months ago

I think you can use customReceivedObject option. Check out the tests and docs.

palbrecht1 commented 7 months ago

@mcollina I believe that in customReceivedObject you don't have access to the request body unless you use the custom serializers, in which case you then ahve the body in all the logs not just the first one