For an HTTP-based, server-side API I have a need to log all the incoming requests, including the request bodies.
My first thought was to use a Plug in the API Pipeline and have that plug do the logging.
The tricky part is logging the request body. From what I can tell you can only retrieve the body from the Plug.Conn once. That means that I would have to retrieve the body in the logging component (using Plug.Conn.read_body/2) and then add the data to the Conn as a custom key or something. This seems "wrong" because it seemingly breaks a contract for any plugs farther down. They can no longer retrieve the body using the same functions. They have to know to look for the body of the request somewhere else.
By the same token, this must be how Plug.Parsers work right? A Plug parser must read the body to parse it. That would imply that any plugs that follow the parser in a pipeline must "know" that the body is no longer available through Plug.Conn.read_body/2.
I guess my question is... is this "normal". Is it common for a Plug to modify the connection in some way that only plugs that are aware of the modification can follow it in a pipeline?
Scott
P.S. It could be the case that handling this level of logging in a Plug is just the Wrong Thing™ to do. I'm certainly open to that possibility too.