Log requests

14 views
Skip to first unread message

Corentin Dupont

unread,
Nov 8, 2018, 5:47:52 PM11/8/18
to haskell...@googlegroups.com
Hi,
I was wondering if it's possible to log requests when they arrive...
I would like to get some logs such as:

Received POST request with data... and headers...

Is it possible to get this in a centralized place, without adding traces in each handlers?
This is my server:

appAPI :: Proxy API
appAPI = Proxy

appServer :: Application
appServer = serve API server


Cheers

Alp Mestanogullari

unread,
Nov 8, 2018, 6:21:30 PM11/8/18
to corenti...@gmail.com, haskell...@googlegroups.com
You'd typically use a WAI middleware for that, like https://hackage.haskell.org/package/wai-extra-3.0.22.1/docs/Network-Wai-Middleware-RequestLogger.html. `Middleware` is just `Application -> Application`, so you can just apply any `Middleware` value (therefore, a function) to the result of calling servant-server's `serve` function on your API type & handlers.

--
You received this message because you are subscribed to the Google Groups "haskell-servant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to haskell-serva...@googlegroups.com.
To post to this group, send email to haskell...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/haskell-servant/CAEyhvmq0JnAjtBmH5C58bHhr%3Dn6SbcAdtNxV%3D6dronr1doVn4w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


--
Alp Mestanogullari

Corentin Dupont

unread,
Nov 28, 2018, 7:21:32 AM11/28/18
to Alp Mestanogullari, haskell...@googlegroups.com
It works great!
For instance, for a parse error I get:
127.0.0.1 - - [28/Nov/2018:13:58:02 +0200] "POST /api/v1/sensors HTTP/1.1" 400 - "http://localhost:8081/swagger-ui/" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:63.0) Gecko/20100101 Firefox/63.0"

But own can I get more details on the parse error?
The command is:
curl -v -X POST "http://localhost:8081/test" -d '{"test": "test"}
Error in $: key "id" not present

How can I get this output (key "id" not present) in the traces as well?
I think this trace is produced by the EASON FromJSON instance.

Cheers


Corentin Dupont

unread,
Nov 28, 2018, 8:09:49 AM11/28/18
to Alp Mestanogullari, haskell...@googlegroups.com, oleg.g...@iki.fi
Another question, I implemented Swagger-UI:
server :: Server API
server = swaggerSchemaUIServer swaggerDoc

swaggerDoc :: S.Swagger
swaggerDoc = toSwagger (Proxy :: Proxy WaziupAPI)
& S.info . S.title .~ "Waziup API"
& S.info . S.version .~ "v2.0.0"
& S.info . S.description ?~ "This is the API of Waziup"
& S.basePath ?~ "/api/v1"
& S.applyTagsFor fooOps ["Foo"]
where
fooOps :: Traversal' S.Swagger S.Operation
fooOps = subOperations (Proxy :: Proxy FooAPI) (Proxy :: Proxy API)


However, the body of the 400 error is not reported at all in the UI (see screenshot).
Screenshot from 2018-11-28 13-25-59.png

I get only the error code and the headers. How can I get the body as well?

Thanks
Corentin
Reply all
Reply to author
Forward
0 new messages