logger
logger copied to clipboard
Fix untracked body size
This PR is based on #86.
Currently there seems to be an assumption that the body will not be modified upstream. This might be intended but, as it is, if koa (or any other upstream middleware) sets the body after the logger finishes, that change will not be reflected in the logged bytes sent.
Even if koa is the only thing upstream, it currently sets a body if none is provided for certain status codes. For example, if the only thing you do is set ctx.status
to 301, koa will add a body of "Moved Permanently"
after all middleware.
The current implementation uses the value in the Content-Length
header after all downstream middleware have finished as the number of bytes sent.
This PR changes this so that it uses the value of the Content-Length
header after the response has been sent/closes. This happens to also be when the actual console.log
occurs.
This doesn't impact the behavior if the body is a stream. It also doesn't change anything if the length is nullish.