Extract logging and stats into a middleware

Instead of capturing stats all over the place, rely on the env object
and middleware placement to capture everything we need to log requests
and responses.

The logger middleware is loaded between the decoder and encoder.