FTM
FTM

Reputation: 2067

How can I record data related to http responses in Laravel?

I wanted to keep records of the http responses my server is throwing. So, for example, for each server request, I would like to record if the http response was a 1xx response, or a 2xx, 3xx and so on. At the end of the day, I would like to see, for example, that from the 1000 requests, 2% of them are a 5xx server error responses. I’m using the laravel framework.

I don’t want to redirect the responses, nor change the response messages. I just want to keep records of them. How could I achieve that? I'm not sure in which point of the laravel model I should introduce my logic. Where to intercept the final response?

Thank you for your help! :)

Upvotes: 0

Views: 178

Answers (2)

FTM
FTM

Reputation: 2067

It seems like I have three options.

  1. (in my opinion, the best option) As mentioned by @mark-davidson, one solution is to use the server logs.
  2. One can also use middleware "after" to achieve that, as mentioned by @peterpan666.
  3. Reading the laravel documentation I just found out about the middleware "terminator", which could also be a good option, since this middleware runs only after the response is sent.

Upvotes: 0

Elie Faës
Elie Faës

Reputation: 3315

You can use a global middleware "after", see this https://laravel.com/docs/5.2/middleware#defining-middleware

Then check the response

  • if it's an instance of \Illuminate\Http\Response, you have a status() method on it that gives you the response status.
  • If it's not a Response instance then it should be a 200 OK status.

Upvotes: 1

Related Questions