承接 hryha/request-logger 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

hryha/request-logger

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

hryha/request-logger 适用场景与选型建议

hryha/request-logger 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 21.75k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2021 年 11 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「log」 「debugging」 「logger」 「laravel」 「request log」 「lumen」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 hryha/request-logger 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 hryha/request-logger 我们能提供哪些服务?
定制开发 / 二次开发

基于 hryha/request-logger 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-11-23