bilfeldt/laravel-route-statistics
Composer 安装命令:
composer require bilfeldt/laravel-route-statistics
包简介
Log statistics about route usage per user/team
README 文档
README
Log Laravel requests and responses for statistical purposes and optionally aggregate by hours/days/months for minimal db requirements.
| Version | Laravel | PHP |
|---|---|---|
| 1.* | 8.* | 9.* | 7.4.* | 8.0.* | 8.1.* |
| 2.* | 10.* | 8.1.* | 8.2.* |
| 3.* | 11.* | 8.1.* | 8.2.* | 8.3.* |
| 4.* | 10.* | 11.* | 12.* | 8.2.* | 8.3.* | 8.4.* | 8.5.* |
Description
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:
- See how much each user uses the application and what part of the application they use
- See if any unauthenticated users are making a lot of requests to your application
Installation
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"
Usage
There are a few ways to enable logging of route usage:
Enable global logging
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 bootstrap/app.php
// bootstrap/app.php <?php use Illuminate\Foundation\Application; use Illuminate\Foundation\Configuration\Exceptions; use Illuminate\Foundation\Configuration\Middleware; return Application::configure(basePath: dirname(__DIR__)) ->withRouting( ... ) ->withMiddleware(function (Middleware $middleware) { // Place the Route middleware before all global middleware $middleware->prepend(\Bilfeldt\LaravelRouteStatistics\Http\Middleware\RouteStatisticsMiddleware::class); }) ->withExceptions(function (Exceptions $exceptions) { ... }) ->create(); ...
Enable via middleware
Instead of adding RouteStatisticsMiddleware as a global middleware then it can be added to certain routes or route groups using:
Route::middleware(['routestatistics'])->...
Enable using request macro
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'); } }
Artisan commands
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.
How it works
This package works as follows:
- Tag the request for logging: Can be done using middleware or request helper
- (optional) Add any context data which will be used when logging: A common use case is adding relevant route parameters like a
team_idfor example - Log the request: Persist the log record to the database - the following will be logged when using the default logger:
user_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})parameters: The route parameters passed (if enabled elsenull)status: The HTTP status (eg202)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...
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
bilfeldt/laravel-route-statistics 适用场景与选型建议
bilfeldt/laravel-route-statistics 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 148.89k 次下载、GitHub Stars 达 242, 最近一次更新时间为 2021 年 04 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「route」 「laravel」 「statistics」 「bilfeldt」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 bilfeldt/laravel-route-statistics 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bilfeldt/laravel-route-statistics 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 bilfeldt/laravel-route-statistics 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Helper to handle stats table with multiple formatter
The "View Counter" bundle
SPSS is a php-based implementation of IBM SPSS Statistics Standard. (Read/write SPSS, PSPP .sav files)
TYPO3 View-Statistics by coding.ms: Logs frontend actions in TYPO3 and displays them in a backend module. It tracks page views, news, downloads, and objects such as products or real estate, while custom objects can be configured via TypoScript. Frontend logins, login duration, user ID, IP address, a
Provide a way to secure accesses to all routes of an symfony application.
Write down your routing mapping at one place
统计信息
- 总下载量: 148.89k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 242
- 点击次数: 40
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-04-19
