xxxcoltxxx/request-logger

此软件包最新版本(1.0.5)没有可用的许可证信息。

记录HTTP请求和错误

1.0.5 2019-01-10 12:58 UTC

This package is auto-updated.

Last update: 2024-09-11 02:11:19 UTC


README

Build Status Latest Stable Version Total Downloads

此软件包允许将请求数据和响应数据发送到外部日志系统。

支持的原生传输方式

  • graylog 传输方式,用于将请求日志发送到 Graylog 服务器
  • log 传输方式,用于将请求日志发送到 Laravel 日志系统
  • null 传输方式,用于在应用程序测试中跳过请求日志

安装

通过 Composer 安装

composer require xxxcoltxxx/request-logger

发布配置文件

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

将异常报告添加到您的 app/Exception/Handler.php 文件中

public function report(Exception $exception)
{
    request_logger()->setException($exception);

    // ...
    parent::report($exception);
}

填写配置

// Enable request logger for all requests without adds middleware
'all_routes' => true,

// Default transport
'default' => env('REQUEST_LOGGER_TRANSPORT', 'graylog'),

// The class for format log message
'formatter' => RequestLogger\RequestFormatter::class,

/*
 * Allowed transports with its configuration.
 * Drivers: 'graylog', 'log', 'null'
 */
'transports' => [

    // The graylog transport
    'graylog' => [
        // The Short message for graylog
        'short_message' => env('GRAYLOG_SHORT_MESSAGE', 'requests'),

        // Limit content size (in bytes). Set false to disable. Graylog has limitations on input messages
        'content_limit' => env('GRAYLOG_CONTENT_LIMIT', 30000),

        // The IP address of the log server
        'host' => env('GRAYLOG_HOST', '127.0.0.1'),

        // The UDP port of the log server
        'port' => env('GRAYLOG_PORT', '12201'),

        // The driver for send requests log to log server
        'driver' => RequestLogger\Transports\GelfUdpTransport::class,
    ],

    // ...
],

将应用程序消息添加到请求日志

您可以使用 request_logger 辅助函数将自定义消息添加到请求数据中。

request_logger()->addMessage('Full name: John Doe')

仅针对特定路由启用日志记录

默认情况下,请求日志记录器在所有路由上启用。您可以在配置文件中禁用 all_routes 选项,并在路由配置中使用 request_logger 中间件。

有关注册中间件的文档,请参阅 Laravel 文档

# config/request_logger.php

return [
    'all_routes' => false,

    // ...
];

# routes/api.php

Route::get('admin/profile', function () {
    //
})->middleware('request_logger');

自定义日志格式

对于自定义日志格式,您可以在配置文件中使用闭包。

# config/request_logger.php

return [
    // ...

    'formatter' => function () {
        $provider  = request_logger();
        $exception = $provider->getException();
        $request   = $provider->getRequest();
        $response  = $provider->getResponse();

        return [
            'uri'           => $request->getRequestUri(),
            'has_exception' => ! is_null($exception),
        ];
    }

    // ...

编写自定义驱动器

要编写自定义驱动器,您必须添加驱动器配置并实现 RequestLogger\Transports\RequestLoggerTransport 接口。

# .env

REQUEST_LOGGER_TRANSPORT=custom

# config/request_logger.php

return [
    // ...

    'transports' => [
        // ...

        'custom' => [
            'driver' => Namespace\CustomTransport::class,
            'custom_option' => 'value',
        ],
    ],
];

在应用程序测试中禁用请求日志记录器

使用 null 驱动器。您可以在 phpunit.xml 配置文件中使用 REQUEST_LOGGER_TRANSPORT 变量。

测试

composer install
vendor/bin/phpunit tests

路线图

  1. 单元测试
  2. 为以下编写文档
    • 自定义日志格式
    • 编写自定义驱动器
  3. 添加 log 驱动器
  4. 添加 null 驱动器
  5. 添加更改日志
  6. 制作视频 "如何与 Graylog 配合使用"