chelout / laravel-http-logger

Laravel 包,用于记录 HTTP 请求、头部信息和会话

v1.4 2019-07-05 12:18 UTC

This package is auto-updated.

Last update: 2024-08-29 05:15:20 UTC


README

Latest Stable Version Total Downloads License

此包提供了一个中间件,用于记录传入的 HTTP 请求数据(正文数据、文件、头部信息和会话数据)。它利用了 Laravel 5.6 日志服务功能。此包可能有助于记录用户对公开 API 的请求。

安装

您可以通过 composer 安装此包

composer require chelout/laravel-http-logger

可选地,您可以使用以下命令发布配置文件

php artisan vendor:publish --provider="Chelout\HttpLogger\HttpLoggerServiceProvider" --tag="config" 

这是发布配置文件的内容

return [
    /*
     * Log file path
     */
    'path' => storage_path('logs/http.log'),
    /*
     * The maximal amount of files to keep (0 means unlimited)
     */
    'max_files' => 5,

    /*
     * Log methods
     * [] - log all methods
     * ['get','post'] - log only 'get' and 'post' methods
     */
    'methods' => [],

    /*
     * Log message format.
     * For for details see https://github.com/Seldaek/monolog/blob/master/doc/01-usage.md#customizing-the-log-format
     * and https://github.com/Seldaek/monolog/blob/master/src/Monolog/Formatter/LineFormatter.php
     */
    'format' => "[%datetime%] %extra.method% %extra.url% from %extra.ips% %context%\n",

    /*
     * Log message datetime format.
     * For for details see https://github.com/Seldaek/monolog/blob/master/doc/01-usage.md#customizing-the-log-format
     * and https://github.com/Seldaek/monolog/blob/master/src/Monolog/Formatter/LineFormatter.php
     */
    'date_format' => null, // "Y-m-d\TH:i:sP"

    /*
     * Log current memory usage
     * @see https://github.com/Seldaek/monolog/blob/master/src/Monolog/Processor/MemoryUsageProcessor.php
     */
    'memory_usage' => true,

    /*
     * Log peak memory usage
     * @see https://github.com/Seldaek/monolog/blob/master/src/Monolog/Processor/MemoryPeakUsageProcessor.php
     */
    'memory_peak_usage' => true,

    /*
     * Log current git branch and commit
     * @see https://github.com/Seldaek/monolog/blob/master/src/Monolog/Processor/GitProcessor.php
     */
    'git' => true,

    /*
     * false - don't log body fields
     * ['only'] - log fields only
     * ['except'] - don't log fields
     *
     * If ['only'] is set, ['except'] parametr will be omitted
     */
    // 'data' => false,
    'data' => [
        'only' => [],
        'except' => [],
    ],

    /*
     * false - don't log uploaded files
     * ['only'] - log files only
     * ['except'] - don't log files
     *
     * If ['only'] is set, ['except'] parametr will be omitted
     */
    // 'files' => false,
    'files' => [
        'only' => [],
        'except' => [],
    ],

    /*
     * false - don't log headers
     * ['only'] - log headers only
     * ['except'] - don't log headers
     *
     * If ['only'] is set, ['except'] parametr will be omitted
     */
    // 'headers' => false,
    'headers' => [
        'only' => ['user-agent'],
        'except' => [],
    ],

    /*
     * false - don't log session
     * ['only'] - log session only
     * ['except'] - don't log session
     *
     * If ['only'] is set, ['except'] parametr will be omitted
     */
    'session' => false,
    // 'session' => [
    //     'only' => [],
    //     'except' => [],
    // ],
];

使用方法

此包提供了一个中间件,可以作为全局中间件或单个路由添加。

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

protected $middleware = [
    // ...
    
    \Chelout\HttpLogger\Middlewares\HttpLogger::class
];
// in a routes file

Route::post('/submit-form', function () {
    //
})->middleware(\Chelout\HttpLogger\Middlewares\HttpLogger::class);

为了记录 HTTP 请求,您应该添加自定义日志通道

// in config/logging.php

return [
    // ...

    'channels' => [
        // ...

        'http-logger' => [
            'driver' => 'custom',
            'via' => \Chelout\HttpLogger\Loggers\HttpLogger::class,
        ],
    ],
];

您还可以通过自定义 Monolog 配置来增强现有的日志通道

// in config/logging.php

return [
    // ...

    'channels' => [
        // ...

        'single' => [
            'driver' => 'single',
            'tap' => [Chelout\HttpLogger\Loggers\MonologCustomizer::class],
            'path' => storage_path('logs/laravel.log'),
            'level' => 'debug',
        ],
    ],
];

待办事项

  • 测试
  • 记录 git 数据?
  • 记录内存使用情况?

测试

composer test

变更日志

请参阅 CHANGELOG 了解最近的变化信息。

灵感来源

此包受到了 Log HTTP requestsLaravel Log EnhancerLaravel 5.6 日志服务 的启发。

贡献

请参阅 CONTRIBUTING 了解详细信息。

致谢

许可证

MIT 许可证(MIT)。请参阅 许可证文件 了解更多信息。