0

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! :)

6
  • Would using "after filters" be the answer? Commented Jan 22, 2016 at 7:46
  • What version of laravel are you using? Commented Jan 22, 2016 at 8:03
  • Are you just wanting to store a count and no other information? Commented Jan 22, 2016 at 9:09
  • Also would it not just be easier to retrieve the information from your server logs. You could write a simple log parser to do this if your just interested in the response codes. Commented Jan 22, 2016 at 9:21
  • @Jeemusu, I'm using Laravel 5.0. Commented Jan 23, 2016 at 8:33

2 Answers 2

1

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.
Sign up to request clarification or add additional context in comments.

2 Comments

what about the routes in which I have a "middleware before". In those cases, would the "middleware after" catch these responses?
Well yeah, that's the beauty of middlewares ;)
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.