Fork me on GitHub
#yada
<
2016-05-31
>
tangrammer11:05:30

Hi guys, today trying a crazy thing again 🙂 I'm struggling with adding a all-handlers logging functionality, I'd like to trace the request and response for each one (including exceptions )

tangrammer11:05:49

so far my way was adding a new step to the default-interceptor-chain

tangrammer11:05:28

and was working fine until i tried to read the body of the request

tangrammer11:05:01

this is my approach

tangrammer11:05:38

if i comment the body related code

(when (= manifold.stream.BufferedStream (type (-> ctx :request :body)))
then it works fine

tangrammer11:05:06

on the contrary i get "error": "clojure.lang.ExceptionInfo: Malformed body {:status 400, :error (not (map? nil))}",

tangrammer11:05:36

so it seems that once i read the body buffer something more happens that break later code

mccraigmccraig12:05:09

@tangrammer: here is our approach to request logging - it operates at the ring-with-maybe-deferred-responses level, rather than as a yada interceptor, since i wanted to log non-yada handlers too - but it may be useful nonetheless - https://www.refheap.com/119793

malcolmsparks12:05:27

@tangrammer: Typically, the body of a request can on ly be read once. It may be large and span multiple buffers, or require multipart decoding. Hence you cannot read the request body more than once during a request. The sames goes for Ring. Where are logging? Current version of yada has an interceptor at the end that you can use (return)

malcolmsparks12:05:27

I mean, it's called 'logging' and it will call any function you put in the resource's :logger entry.

malcolmsparks12:05:01

It is also called in the error chain, so you can use it for logging exceptions too

malcolmsparks12:05:23

I added this because of a request from @shmish111 and he has been able to use it

tangrammer12:05:29

Great help! Thanks guys!!

tangrammer13:05:12

Hi @malcolmsparks: using your approach

(defn log-handler  [ctx]

   (log/info "HANDLER LOGGER : " (:method ctx) (-> ctx :request :uri) " :::: " (-> ctx :parameters) )
   ctx

  )
{ ;; the yada resource here...
...
    :logger log-handler

...
}
I don't get the parameters that i should have in this loggin step of the default-interceptor-chain as far as this step is after parse-parameters

tangrammer13:05:33

I mean that (-> ctx :parameters) always returns nil

tangrammer13:05:21

anyway i get the logger log/info calls but no parameters attached to the ctx as normally i have inside the response fn for each resource

tangrammer13:05:39

^i hope i could explain it well

tangrammer14:05:04

@malcolmsparks: finally it seems an issue I'll create a github one with an reproducible example

tangrammer14:05:09

if an error is thrown inside my response then my (-> ctx :parameters :body) => nil even if the i/process-request-body is passed through

tangrammer22:05:44

@malcolmsparks: sorry for the noise, definitely the step wasn't reached in the interceptor chain and my problem was related with the schema validation I couldn't realise this error because i couldn't read the response/body in the logging interceptor-step without leaving unreadable the response to final api client (in this case a mobile client) Anyway, yada works fine 🙂 thanks!