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

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

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

hryha/laravel-request-logger

Composer 安装命令:

composer require hryha/laravel-request-logger

包简介

A Laravel 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 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
  • Duplicated requests detection
  • Configurable data retention period
  • Sensitive data masking
  • Support for custom logging fields
  • Sampling support - log only a percentage of requests
  • Smart filtering - always log slow requests and requests with high memory usage

Requirements

  • PHP 8.2 or higher
  • Laravel 11 or higher

Installation

Install the package via Composer:

composer require hryha/laravel-request-logger

After installing Request Logger, publish its assets and config using the request-logs:install Artisan command:

php artisan request-logs:install

View the complete configuration options: here.

After installing Request Logger, you should also run the migrate command in order to create the tables needed to store Request Logger's data:

php artisan migrate

Usage

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

// In bootstrap/app.php for global middleware
return Application::configure(basePath: dirname(__DIR__))
    ->withMiddleware(function (Middleware $middleware): void {
        $middleware->prepend([
            \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);

Upgrading

After upgrading to any new Request Logger version, you should re-publish Request Logger's assets:

php artisan request-logs:publish

Data Pruning

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

use Illuminate\Support\Facades\Schedule;

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. Additional data can be added from anywhere in the application using this code:

use Hryha\RequestLogger\RequestLogger;

resolve(RequestLogger::class)->addCustomField('user_id', Auth::id());

also, to filter logs by this field, you can add this field to the settings

REQUEST_LOGGER_CUSTOM_FIELDS="user_id,other_field"

Sampling Configuration

To reduce storage usage and improve performance on high-traffic applications, you can configure Request Logger to log only a percentage of requests.

Basic Sampling

Enable sampling in your .env file:

# Enable sampling
REQUEST_LOGGER_SAMPLING_ENABLED=true
# Log only 0.5% of successful requests (2xx)
REQUEST_LOGGER_SAMPLING_2XX=0.5
# Log 50% of redirects (3xx)
REQUEST_LOGGER_SAMPLING_3XX=50
# Log all client errors (4xx)
REQUEST_LOGGER_SAMPLING_4XX=100
# Log all server errors (5xx)
REQUEST_LOGGER_SAMPLING_5XX=100

Note: Sampling rates support up to 2 decimal places (e.g., 10.25%). Values with more precision are automatically rounded.

Smart Filtering

Always log critical requests regardless of sampling rate:

# Always log requests slower than 500ms
REQUEST_LOGGER_ALWAYS_LOG_SLOW=500
# Always log requests using more than 30MB memory
REQUEST_LOGGER_ALWAYS_LOG_HEAVY_MEMORY=30

How Sampling Works

When sampling is enabled:

  1. Ignored paths are checked first (never logged)
  2. Smart filters are applied (slow requests, high memory usage)
  3. If no smart filter matches, sampling rate is applied randomly
  4. A value of 0 means never log, 100 means always log

Ignoring Paths

Configure paths to ignore by setting REQUEST_LOGGER_IGNORE_PATHS in your .env file:

REQUEST_LOGGER_IGNORE_PATHS="telescope*,horizon*,nova-api*"

These paths will never be logged, regardless of sampling configuration.

Panel authorization

Be sure to protect this panel from unauthorized access. We recommend using Basic Auth middleware or something similar. To do this, add an auth middleware in config/request-logger.php

'middleware' => [
    \Hryha\SimpleBasicAuth\SimpleBasicAuth::class,
],

Testing

Run the test:

composer test

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

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 11
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-03-20