I currently run the middleware logStdoutDev from Network.Wai.Middleware.RequestLogger, but it only logs the path and the Accept header (possibly other headers too). I want to see the body of the POST and PUT requests as well. This body happens to be json, so just printing it to stdout will do fine.
I have searched for a WAI middleware that logs everything but have not found any. I don't really know enough about WAI internals to write something that extracts POST body and then puts it back in myself, so I was hoping to avoid that learning curve right now.
WAI Middleware
is just a transformation over Application
:
type Middleware = Application -> Application
And Application
is just a handler:
type Application = Request -> (Response -> IO ResponseReceived) -> IO ResponseReceived
All you need to do is define handler that will log whatever you want and delegate "real work" downstream:
-- note that this is equivalent to Application -> Application
logAllMiddleware :: Application -> Request -> (Response -> IO ResponseReceived) -> IO ResponseReceived
logAllMiddleware app req respond = do
print . unpack . requestBody req
app req respond
Please keep in mind that I wrote this code without access to ghc. It may not be completely correct.
This functionality is provided from the wai-extra
package.
{-# LANGUAGE OverloadedStrings #-}
import Web.Scotty
import Network.HTTP.Types
import Network.Wai.Middleware.RequestLogger
main = scotty 3000 $ do
middleware logStdoutDev
get "/" $ do
text "example"
Example output:
Setting phasers to stun... (port 3000) (ctrl-c to quit)
GET /
Accept: */*
Status: 200 OK 0.000050947s
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With