How to log or record 404 errors in Laravel

January 26, 2021

When I moved my personal website to Laravel, I wanted to make sure that I hadnā€™t forgotten to move some important routes. Therefore, I wanted to keep a log of all the ā€œ404 not foundā€ errors that happened on my website.

I was originally going to listen for the error to be thrown within the app, but then found out that Laravel ignores 404 errors for you.

Instead, I settled on a hacky (but simple and effective!) solution:

You can customize Laravelā€™s 404 error page by creating a view at resources/views/errors/404.blade.php. I built a simple ā€œnot foundā€ page there, and then wrote the PHP code I wanted to run in the view within a @php blade directive in that view:

{{-- resources/views/errors/404.blade.php --}}
@php
  $array = Cache::get('404') ?? []; $array[] = '/' . request()->path(); Cache::forever('404', $array);
@endphp

This code adds the current URL to an array thatā€™s stored in the cache, so I can see which URLs people are going to but that donā€™t exist.