aymanalhattami/log-request-response

记录请求、响应、头部信息、方法、IP 地址和 URL

0.0.23 2024-04-02 11:08 UTC

This package is auto-updated.

Last update: 2024-09-02 17:53:01 UTC


README

Latest Version on Packagist Total Downloads

记录请求、响应、头部信息、方法、IP 地址和 URL

注意: 日志对于调试问题非常有价值。通过审查请求和响应数据,开发人员可以追踪发送和接收的数据,并确定问题可能发生的地方。

  • 适用于记录 API 请求和响应
  • 启用/禁用请求记录
  • 启用/禁用响应记录
  • 启用/禁用头部记录
  • 您可以选择要记录哪些请求数据
  • 您可以选择要记录哪些响应数据
  • 您可以选择要记录哪些 URL

安装

您可以通过 composer 安装此包

composer require aymanalhattami/log-request-response

用法

记录请求和响应

use Illuminate\Support\Facades\Route;
use AymanAlhattami\LogRequestResponse\Http\Middleware\LogRequestResponseMiddleware;
use App\Http\Controllers\ExampleController;

# Log all routes
Route::middleware(LogRequestResponseMiddleware::class)->group(function() {
    // routes
});

# Log single route
Route::get('example', ExampleController::class)->middleware(LogRequestResponseMiddleware::class);

记录请求

use Illuminate\Support\Facades\Route;
use AymanAlhattami\LogRequestResponse\Http\Middleware\LogRequestMiddleware;
use App\Http\Controllers\ExampleController;

# Log all routes
Route::middleware(LogRequestMiddleware::class)->group(function() {
    // routes
});

# Log single route
Route::get('example', ExampleController::class)->middleware(LogRequestMiddleware::class);

记录响应

use Illuminate\Support\Facades\Route;
use AymanAlhattami\LogRequestResponse\Http\Middleware\LogResponseMiddleware;
use App\Http\Controllers\ExampleController;

# Log all routes
Route::middleware(LogResponseMiddleware::class)->group(function() {
    // routes
});

# Log single route
Route::get('example', ExampleController::class)->middleware(LogResponseMiddleware::class);

记录头部信息

use Illuminate\Support\Facades\Route;
use AymanAlhattami\LogRequestResponse\Http\Middleware\LogHeadersMiddleware;
use App\Http\Controllers\ExampleController;

# Log all routes
Route::middleware(LogHeadersMiddleware::class)->group(function() {
    // routes
});

# Log single route
Route::get('example', ExampleController::class)->middleware(LogHeadersMiddleware::class);

配置

return [
    /* Defines the logging level for recording request and response logs. Default: 'info'. */
    'log_level' => env('REQUEST_RESPONSE_LOG_LEVEL', 'info'),

    'request' => [
        /* Determines whether logging of HTTP requests is enabled. */
        'enabled' => env('LOG_REQUEST', true),

        /* Title or prefix for request logs, making them easily identifiable. */
        'title' => env('LOG_REQUEST_TITLE', 'Request'),

        /* Specifies whether headers should be included in the request log. */
        'headers' => env('LOG_HEADERS_WITH_REQUEST', true),

        /* Indicates whether the request URL should be logged. */
        'url' => env('LOG_REQUEST_URL', true),

        /* Determines whether the HTTP method should be logged. */
        'method' => env('LOG_REQUEST_METHOD', true),

        /* Specifies whether the IP address of the requester should be logged. */
        'ip' => env('LOG_REQUEST_IP', true),

        /* Indicates whether information about the authenticated user should be included in the request log. */
        'auth_user' => env('LOG_AUTH_USER_WITH_REQUEST', true),

        /* Specifies whether a unique identifier for the request should be logged. */
        'request_id' => env('LOG_REQUEST_ID_WITH_REQUEST', true),

        /**
         * Specifying which request data should be logged.
         * if 'only' has data, then 'except' will be ignored.
         * if 'except' has data, then 'only' will be ignored.
         * if both 'only' and 'except' are empty, then all data will be logged.
         * if both 'only' and 'except' are not empty, then only the data specified in 'only' will be logged.
         * if 'only' is empty, but 'except' is not empty, then all data except the data specified in 'except' will be logged.
         * if 'only' is not empty, but 'except' is empty, then only the data specified in 'only' will be logged.
         */
        'data' => [
            'only' => [],
            'except' => ['password', 'password_confirmation'],
        ],

        /**
         * Specifying which URLs should be logged.
         * if 'only' has data, then 'except' will be ignored.
         * if 'except' has data, then 'only' will be ignored.
         * if both 'only' and 'except' are empty, then all URLs will be logged.
         * if both 'only' and 'except' are not empty, then only the URLs specified in 'only' will be logged.
         * if 'only' is empty, but 'except' is not empty, then all URLs except the URLs specified in 'except' will be logged.
         * if 'only' is not empty, but 'except' is empty, then only the URLs specified in 'only' will be logged.
         */
        'urls' => [
            'only' => [],
            'except' => [],
        ]
    ],

    'response' => [
        /* Determines whether logging of HTTP responses is enabled. */
        'enabled' => env('LOG_RESPONSE', true),

        /* Title or prefix for response logs. */
        'title' => env('LOG_RESPONSE_TITLE', 'Response'),

        /* Indicates whether information about the authenticated user should be included in the response log. */
        'auth_user' => env('LOG_AUTH_USER_WITH_RESPONSE', true),

        /* Specifies whether the request's unique identifier should be logged with the response. */
        'request_id' => env('LOG_REQUEST_ID_WITH_RESPONSE', true),

        /**
         * Specifying which response data should be logged.
         * if 'only' has data, then 'except' will be ignored.
         * if 'except' has data, then 'only' will be ignored.
         * if both 'only' and 'except' are empty, then all data will be logged.
         * if both 'only' and 'except' are not empty, then only the data specified in 'only' will be logged.
         * if 'only' is empty, but 'except' is not empty, then all data except the data specified in 'except' will be logged.
         * if 'only' is not empty, but 'except' is empty, then only the data specified in 'only' will be logged.
         */
        'data' => [
            'only' => [],
            'except' => [],
        ]
    ],

    'headers' => [
        /* Specifies whether header logging is enabled. */
        'enabled' => env('LOG_HEADERS', true),

        /* Title or prefix for header logs. */
        'title' => env('LOG_HEADERS_TITLE', 'Headers'),

        /* Indicates whether information about the authenticated user should be included in the headers log. */
        'auth_user' => env('LOG_AUTH_USER_IN_HEADERS', true),
    ],

    /* which data of authenticated user to be logged. Default: 'email'. */
    'auth_user_column' => env('LOG_AUTH_USER_COLUMN', 'email'),

    /* which guard of authenticated user to be logged. Default: 'api'. */
    'auth_user_guard' => env('LOG_AUTH_USER_GUARD', 'api'),
];

测试

composer test

变更日志

请参阅 变更日志 了解最近的变化。

贡献

请参阅 贡献指南 了解详情。

安全

如果您发现任何安全相关问题,请通过电子邮件 ayman.m.alhattami@gmail.com 而不是使用问题追踪器。

致谢

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件

Laravel 包模板

此包是使用 Laravel 包模板 生成的。