Log requests

Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Log requests

Corentin Dupont
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

--
Reply | Threaded
Open this post in threaded view
|

Re: Log requests

Alp Mestanogullari
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.

On Thu, Nov 8, 2018 at 11:47 PM Corentin Dupont <[hidden email]> wrote:
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

--
Reply | Threaded
Open this post in threaded view
|

Re: Log requests

Corentin Dupont
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



On Fri, Nov 9, 2018 at 1:21 AM Alp Mestanogullari <[hidden email]> wrote:
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.

On Thu, Nov 8, 2018 at 11:47 PM Corentin Dupont <[hidden email]> wrote:
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

--
Reply | Threaded
Open this post in threaded view
|

Re: Log requests

Corentin Dupont
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

On Wed, Nov 28, 2018 at 2:21 PM Corentin Dupont <[hidden email]> wrote:
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



On Fri, Nov 9, 2018 at 1:21 AM Alp Mestanogullari <[hidden email]> wrote:
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.

On Thu, Nov 8, 2018 at 11:47 PM Corentin Dupont <[hidden email]> wrote:
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

--