定制 hryha/request-logger 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

hryha/request-logger

最新稳定版本:v1.0.28

Composer 安装命令:

composer require hryha/request-logger

包简介

A Laravel/Lumen package to log requests and responses

README 文档

README

Latest Version on Packagist Total Downloads PHP Version Laravel Version Software License

Laravel request logger

Overview

This package provides middleware that logs incoming HTTP requests and responses in Laravel/Lumen applications. You can view your logs through a dedicated panel at https://your.domain/request-logs.

Features

  • HTTP request and response logging
  • Web-based log viewer interface
  • Configurable data retention period
  • Sensitive data masking
  • Support for custom logging fields
  • Multiple storage drivers (File system, MySQL)

Requirements

  • PHP 7.3 or higher
  • Laravel/Lumen 5.7 or higher
  • MySQL 5.7 or higher (required for database driver)

Laravel Installation

Install the package via Composer::

composer require hryha/request-logger 

Run the database migrations:

 php artisan migrate 

Optionally, publish the package's configuration file:

php artisan vendor:publish --provider="Hryha\RequestLogger\RequestLoggerServiceProvider" 

View the complete configuration options: here.

The package provides middleware that can be registered either globally or on specific routes.

// In app/Http/Kernel.php for global middleware protected $middleware = [ // ... \Hryha\RequestLogger\Http\Middleware\RequestLogger::class ]; 
// Or in your routes file for specific routes Route::get('/user', [UserController::class, 'index'])->middleware(\Hryha\RequestLogger\Http\Middleware\RequestLogger::class); 

Lumen Installation

Install the package via Composer:

composer require hryha/request-logger 

First, install the vendor:publish plugin.

Register the service provider:

//In 'bootstrap/app.php' $app->register(\Hryha\RequestLogger\RequestLoggerServiceProvider::class); 

Optionally, publish the configuration file:

php artisan vendor:publish --provider="Hryha\RequestLogger\RequestLoggerServiceProvider" 

If you publish the configuration, register it in your application:

// In 'bootstrap/app.php' $app->configure('request-logger'); 

Run the database migrations:

php artisan migrate 

Register the request logger middleware:

// In 'bootstrap/app.php' $app->routeMiddleware([ // ... 'request-logger' => \Hryha\RequestLogger\Http\Middleware\RequestLogger::class, ]); 

Use the middleware on specific routes:

// In your routes file Route::post('/test', ['uses' => 'TestController@test', 'middleware' => ['request-logger']]); 

Data Pruning

To prevent the request_logs table from growing too large, schedule the request-logs:clear command to run daily:

$schedule->command('request-logs:clear')->daily(); 

The request-logs:clear command removes logs older than the number of days specified in your log_keep_days configuration. To delete all logs, use the --all parameter:

php artisan request-logs:clear --all 

Custom Fields

The Request Logger supports additional custom fields for enhanced logging capabilities.

Use the RequestLogger::addCustomField(key, value) method to include additional data in your logs. Ensure that any custom fields are properly defined in the request_logs database table.

Implementation Example

Follow these steps to implement custom fields:

  1. Create a middleware class (e.g., ConfigureRequestLogger)
  2. Implement the handle method using RequestLogger::addCustomField(key, value)
  3. Register the middleware in your Http\Kernel.php file to activate it.

Example middleware implementation:

<?php namespace App\Http\Middleware; use App\Models\User; use Closure; use Hryha\RequestLogger\RequestLogger; use Illuminate\Http\Request; final class ConfigureRequestLogger { private RequestLogger $requestLogger; public function __construct(RequestLogger $requestLogger) { $this->requestLogger = $requestLogger; } public function handle(Request $request, Closure $next) { if (config('request-logger.enabled')) { /** @var User|null $user */ $user = $request->user(); // Log the authenticated user's ID $this->requestLogger->addCustomField('user_id', $user?->id); } return $next($request); } } 

Ignoring Responses by Status Code

Configure status codes to ignore by setting REQUEST_LOGGER_IGNORE_RESPONSE_STATUSES in your .env file. The setting accepts both status ranges and specific status codes:

REQUEST_LOGGER_IGNORE_RESPONSE_STATUSES="[[100, 299], 301, 302]" 

This configuration will ignore logs for responses with status codes between 100-299, as well as 301 and 302 responses.

Testing

Run the test:

php vendor/bin/phpunit 

统计信息

  • 总下载量: 18.31k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固