Package Data | |
---|---|
Maintainer Username: | bilfeldt |
Maintainer Contact: | abilfeldt@gmail.com (Anders Bilfeldt) |
Package Create Date: | 2021-04-18 |
Package Last Update: | 2024-07-17 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-14 15:04:05 |
Package Statistics | |
---|---|
Total Downloads: | 81,360 |
Monthly Downloads: | 3,436 |
Daily Downloads: | 188 |
Total Stars: | 228 |
Total Watchers: | 3 |
Total Forks: | 23 |
Total Open Issues: | 1 |
Log Laravel requests and responses for statistical purposes and optionally aggregate by hours/days/months for minimal db requirements.
Log requests and group them together for aggregated statistics of route usage. Grouping requests by route means that this package saves a minimum of data to the database and subsequent purging of old data can improve this even further.
This package lets you:
You can install the package via composer:
composer require bilfeldt/laravel-route-statistics
You can publish and run the migrations with:
php artisan vendor:publish --provider="Bilfeldt\LaravelRouteStatistics\LaravelRouteStatisticsServiceProvider" --tag="migrations"
php artisan migrate
You can publish the config file with:
php artisan vendor:publish --provider="Bilfeldt\LaravelRouteStatistics\LaravelRouteStatisticsServiceProvider" --tag="config"
There are a few ways to enable logging of route usage:
This will enable site-wide logging and although being the easiest implementation this might not be exactly what you are looking for (consider only logging relevant routes using the middleware approach below)
Simply add RouteStatisticsMiddleware
as a global middleware in app/Http/Kernel.php
// app/Http/Kernel.php
<?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* @var array
*/
protected $middleware = [
\Bilfeldt\LaravelRouteStatistics\Http\Middleware\RouteStatisticsMiddleware::class, // <-- 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,
];
...
Instead of adding RouteStatisticsMiddleware
as a global middleware then it can be added to certain routes or route groups using:
Route::middleware(['routestatistics'])->...
It is possible to enable logging ad-hoc, usually within a controller, which is useful for any conditional logging:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function index(Request $request)
{
$request->routeStatistics(); // This will enable route statistics logging
return view('home');
}
}
This package comes with two neat Artisan commands:
route:stats
: An easy way to see route statistics for certain relevant routes.route:unused
: A neat way to list the routes without any logs. Be aware that the routes should also be logged for this to be useful.This package works as follows:
team_id
for exampleuser_id
: The authenticated user (if any)team_id
: The team id associated with the request (if available)method
: The HTTP method (GET/POST/...
)route
: The route name (if available) or the route URI (eg /posts/{post}
)status
: The HTTP status (eg 202
)ip
: The request ipdate
: The date of the request as datetime (can be aggregated)counter
: Number of requests logged when aggregating records by minute/hour/day/month...composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.