Skip to content

Laravel Log Throttled Users

Sep 1, 2024 2 min read #laravel #security

This post is more than 18 months old. Technology changes quickly, so some of this content may be out of date (but that's not always the case). Please verify any technical or programming information against the current release.

You’ve got Laravel throttling set up on authentication, password reset and other sensitive endpoints. But, how do you know this is actually working to stop people? Or what if you either want to admonish bad users or proactively reach out with support to help them? Perhaps you might want to log your throttled attempts. It’s pretty easy.

So, in our example, we’re just going to send an info log. But, you might find yourself doing other things with this information (like sending it over to bug reporting tools or trend analysis / BI tools.) Let’s take a look on how we can do this.

First, find your App\Exceptions\Handler file and go to the register() method. In there, we want to catch reportable exceptions of the throttle type, and do something with them. Check this out.

$this->reportable(function (\Illuminate\Http\Exceptions\ThrottleRequestsException $e) {
  \Log::info('Request throttled', [
    'input' => \Request::input(),
    'ip' => \Request::ip(),
    'url' => \Request::url(),
  ]);
});
php

This handles whenever a ThrottleRequestsException has been reported. You can inspect that class for more details about the context you get. Then, while it is being thrown, we also log to our standard info logger with context of the input, ip and URL. You may want to filter this information or use different logging facilities. This is just one example of how you can do this.

I do contract work - and specialize in Laravel. If you need a hand, let me know.