dmeys/request-logger 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

dmeys/request-logger

最新稳定版本:v1

Composer 安装命令:

composer require dmeys/request-logger

包简介

A Laravel/Lumen package to log requests and responses

README 文档

README

Laravel request logger

Log request and response

This package adds a middleware which can log incoming requests and responses. You can see the logs in view panel at https://your.domain/request-logs

Laravel Installation

You can install the package via composer:

composer require dmeys/request-logger

You must publish the asset files with:

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

You must publish the config file and assets with:

request-logger.php
return [
    'locale' => env('REQUEST_LOGGER_LOCALE', 'en'), // available 'en', 'ua'
    'middleware' => [
        \Dmeys\RequestLogger\Http\Middleware\BaseAuth::class,
    ],
    'base_auth' => [
        'login' => env('REQUEST_LOGGER_LOGIN', 'login'),
        'password' => env('REQUEST_LOGGER_PASSWORD', 'password'),
    ],
    'timezone' => env('REQUEST_LOGGER_TIMEZONE', env('APP_TIMEZONE', config('app.timezone', 'UTC'))),
    'date_format' => env('REQUEST_LOGGER_DATE_FORMAT', 'Y-m-d'),
    'time_format' => env('REQUEST_LOGGER_TIME_FORMAT', 'H:i:s.u'),
    'log_keep_days' => env('REQUEST_LOGGER_KEEP_DAYS', 14),
    'table_name' => 'request_logs',
    'enabled' => env('REQUEST_LOGGER_ENABLED', true),
    'ignore_paths' => [
        'request-logs*',
        'telescope*',
        'nova-api*',
    ],
    'hide_fields' => [
        'request' => [
            'headers' => [
                'authorization',
                'php-auth-user',
                'php-auth-pw',
            ],
            'content' => [
                'password',
                'token',
                'access_token',
            ],
        ],
        'response' => [
            'content' => [
                'password',
                'token',
                'access_token',
            ],
        ],
    ],
    'replacer_hidden_fields' => '|^_-|',
];

You have to execute migrations with:

    php artisan migrate --path=/vendor/dmeys/request-logger/database/migrations/2021_11_05_000000_create_request_log_fingerprints_table.php
    php artisan migrate --path=/vendor/dmeys/request-logger/database/migrations/2021_11_05_000000_create_request_logs_table.php

This packages provides a middleware which can be added as a global middleware or as a single route.

// in `app/Http/Kernel.php`

protected $middleware = [
    // ...
    
    \Dmeys\RequestLogger\Http\Middleware\RequestLogger::class
];
// in a routes file

Route::post('/test', function () {
    //
})->middleware(\Dmeys\RequestLogger\Http\Middleware\RequestLogger::class);

Supported drivers

  • storage logs
  • database (mysql)

Lumen Installation

You can install the package via composer:

composer require dmeys/request-logger --dev

You must install vendor:publish plugin

You must register provider:

//in 'bootstrap/app.php'

$app->register(\Dmeys\RequestLogger\RequestLoggerServiceProvider::class); 

You must publish the config file and assets with:

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

This is the contents of the published config file:

request-logger.php
return [
    'locale' => env('REQUEST_LOGGER_LOCALE', 'en'), // available 'en', 'ua'
    'middleware' => [
        \Dmeys\RequestLogger\Http\Middleware\BaseAuth::class,
    ],
    'base_auth' => [
        'login' => env('REQUEST_LOGGER_LOGIN', 'login'),
        'password' => env('REQUEST_LOGGER_PASSWORD', 'password'),
    ],
    'timezone' => env('REQUEST_LOGGER_TIMEZONE', env('APP_TIMEZONE', config('app.timezone', 'UTC'))),
    'date_format' => env('REQUEST_LOGGER_DATE_FORMAT', 'Y-m-d'),
    'time_format' => env('REQUEST_LOGGER_TIME_FORMAT', 'H:i:s.u'),
    'log_keep_days' => env('REQUEST_LOGGER_KEEP_DAYS', 14),
    'table_name' => 'request_logs',
    'enabled' => env('REQUEST_LOGGER_ENABLED', true),
    'ignore_paths' => [
        'request-logs*',
        'telescope*',
        'nova-api*',
    ],
    'hide_fields' => [
        'request' => [
            'headers' => [
                'authorization',
                'php-auth-user',
                'php-auth-pw',
            ],
            'content' => [
                'password',
                'token',
                'access_token',
            ],
        ],
        'response' => [
            'content' => [
                'password',
                'token',
                'access_token',
            ],
        ],
    ],
    'replacer_hidden_fields' => '|^_-|',
];

You must register this config file:

//in 'bootstrap/app.php'

$app->configure('request-logger');

You must execute migrations with:

php artisan migrate

You must create storage symbolic link with:

php artisan storage:link

You must register middleware:

// in 'bootstrap/app.php'

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

This packages provides a middleware which can be added as a global middleware or as a single route.

// in a routes file

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

Data Pruning

Without pruning, the request_logs table can accumulate records very quickly. To mitigate this, you should schedule the request-logs:clear artisan command to run daily:

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

Running the php artisan request-logs:clear command deletes recorded logs older than the number of days specified in the log_keep_days configuration.

To delete all logs, add the "--all" parameter php artisan request-logs:clear --all

Configuration custom fields

The Request Logger allows you to add custom fields for logging. The package provides the configureRequestLoggerCustomFields function, which must return an array containing the custom fields. Additionally, make sure to add these custom fields to the database table request_logs.

Here's an example of using configureRequestLoggerCustomFields():

  • Add the middleware ConfigureRequestLoggerMiddleware.
  • In the middleware, implement the handle method and specify custom fields using the configureRequestLoggerCustomFields function.
final class ConfigureRequestLoggerMiddleware
{
	public function handle(Request $request, Closure $next)
	{
		configureRequestLoggerCustomFields(function () use ($request) {
			return ['user_id' => $request->user()->id ?? null];
		});

		return $next($request);
	}
}
  • Register the middleware in the Http Kernel.

These steps outline how to use the configureRequestLoggerCustomFields function to add custom fields to your logging setup.

Testing

php vendor/bin/phpunit

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-08-28

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固