How to log response headers like pino-http #4833
Unanswered
gabrieledarrigo
asked this question in
Help
Replies: 1 comment
-
The issue happens because we write headers on the response via the You can work around it as follows. response.response.on('finish', () => {
const level = this.getLogLevel(response.response.statusCode)
const req = stdSerializers.req(request.request)
const res = stdSerializers.res(request.response)
res.headers = response.getHeaders() as any
this.logger[level]({
req,
res,
})
}) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm working with Adonis.js 6 and trying to log every HTTP request and response, similar to how pino-http does it.
While requests are logged correctly, responses have empty headers when using
response.response.getHeaders()
.Here are the details:
Setup: I log requests and responses using a custom middleware. To capture logs, it listens to the
finish
events on theresponse
object.Problem: Headers in the response are empty (
response.response.getHeaders()
returns{}
).However, the private property
_headers
contains the expected data.Here's the middleware code:
The middleware is configured on the Server stack.
How can I correctly access response headers without relying on
_headers
?Is there a better way to implement this logging middleware in Adonis.js?
Beta Was this translation helpful? Give feedback.
All reactions