Logging stats of all requests to your sites can help you do the following:
In turn, logging stats gives us statistical data that helps show us how to improve the site.
laravel-route-statistics
packageIn this shot, we will use the laravel-route-statistics
package to allow us to add the user monitoring functionality.
laravel-route-statistics
basically enables us to log requests and responses from our application.
Run the following command in your command line to install the package and ensure your network is strong during this process.
composer require bilfeldt/laravel-route-statistics
php artisan vendor:publish --provider="Bilfeldt\LaravelRouteStatistics\LaravelRouteStatisticsServiceProvider" --tag="migrations"
php artisan migrate
php artisan vendor:publish --provider="Bilfeldt\LaravelRouteStatistics\LaravelRouteStatisticsServiceProvider" --tag="config"
In my previous shot, we saw how to do log stats from a controller, which enabled us to get login information for a specific function in a controller.
In this shot, we will log the entire request to our site.
We do that from the app/Http/Kernel.php
by registering/adding the RouteStatisticsMiddleware
middleware, like so:
// app/Http/Kernel.php<?phpnamespace App\Http;use Illuminate\Foundation\Http\Kernel as HttpKernel;class Kernel extends HttpKernel {protected $middleware = [\Bilfeldt\LaravelRouteStatistics\Http\Middleware\RouteStatisticsMiddleware::class, // <-- The middleware we added// \App\Http\Middleware\TrustHosts::class,\App\Http\Middleware\TrustProxies::class,\Fruitcake\Cors\HandleCors::class,\App\Http\Middleware\PreventRequestsDuringMaintenance::class,\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,\App\Http\Middleware\TrimStrings::class,\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,];
The code returns the frequency of the route being visited, which is then used graphically, like so:
The graph depends on how you want to represent your data.
Notice how we add the middleware at the top. This is to ensure that it logs every request first before other middleware interacts with the request.