pino-http icon indicating copy to clipboard operation
pino-http copied to clipboard

Logging responseTime in the message

Open francisdb opened this issue 2 years ago • 5 comments

Use case

We want to log request time in the log message

Code

function customSuccessMessage (res) {
  return `request completed ${res.req.method} ${res.req.url} ${res.statusCode} ${???.responseTime}ms`
}

we are looking for something we can put at ???

Context

Of course we want this responseTime to be the same as what is added in the log context

https://github.com/pinojs/pino-http/blob/5be1d8d9002f3ea9f4588dcd96826c7554e799c4/logger.js#L117-L119

As far as I can see this is not available in the context of customErrorMessage/customSuccessMessage, unless you duplicate the code that adds it to the log line context. With a chance of both values to be different.

https://github.com/pinojs/pino-http/blob/8232e17f32e6852422e84707fef99e298910f7ad/logger.js#L91

francisdb avatar Feb 27 '22 21:02 francisdb

I don't understand this issue, could you please clarify?

mcollina avatar Mar 02 '22 23:03 mcollina

I don't understand this issue, could you please clarify?

Should indeed have done a better job describing the issue, I edited the description.

francisdb avatar Mar 03 '22 09:03 francisdb

Good spot! Would you like to send a PR?

mcollina avatar Mar 03 '22 09:03 mcollina

How do you see this passed to the customSuccessMessage function? Some kind of extra context object argument?

francisdb avatar Mar 03 '22 14:03 francisdb

yes, exactly. Maybe just an added parameter?

mcollina avatar Mar 03 '22 14:03 mcollina