定制 robmellett/http-logging 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

robmellett/http-logging

最新稳定版本:2.0.4

Composer 安装命令:

composer require robmellett/http-logging

包简介

A small lightweight package to log all Guzzle http request and responses.

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

A small lightweight package to log all Guzzle http request and responses.

Installation

You can install the package via composer:

composer require robmellett/http-logging

You can publish the config file with:

php artisan vendor:publish --tag="http-logging-config"

This is the contents of the published config file:

<?php

// config for RobMellett/HttpLogging
return [
    /*
     *  The Laravel Log Channel to send logs to.
     */
    'channel' => 'http_logs',

    /*
     * Customize how the Secure Json Formatter redacts secrets.
     */
    'secure_json_formatter' => [

        /*
         * Secret Values will be replaced with this value.
         */
        'redacted_value' => '[--REDACTED--]',

        /*
         * Specific values to redact from the logs.
         */
        'secrets' => [
            // e.g
            // env('API_SECRET'),
        ],

        /*
         * Regular expressions to redact from the logs.
         */
        'regexes' => [
            '/Bearer\s\w+/',
        ],
    ],
];

Usage

You can add this middleware to the Laravel Http Client or Guzzle in the following way.

use RobMellett\HttpLogging\HttpLogging;

Http::withMiddleware(new HttpLogging())
    ->asJson()
    ->get('https://jsonplaceholder.typicode.com/posts');

You can configure the Log Formatter by adding the following to the Laravel logging config file.

// config/logging.php

'channels' => [
    // ...Previous config
    
    'http_logs' => [
        'driver' => 'single',
        'path' => storage_path('logs/laravel.log'),
        'level' => 'debug',

        // This will remove sensitive values such as "key", "secret", "hash", "token" from the logs
        'formatter' => RobMellett\HttpLogging\Support\SecureJsonFormatter::class
        
        // Or if you would prefer to send sensitive data to the logs
        //'formatter' => Monolog\Formatter\JsonFormatter::class,
    ],
]

Which will send the following http request/response info to the logs.

A Http Request

{
  "message": "Request 0b65fca7-a768-4832-8401-da52aa2885a9",
  "context": {
    "request_id": "0b65fca7-a768-4832-8401-da52aa2885a9",
    "method": "GET",
    "uri": {
      "scheme": "https",
      "host": "jsonplaceholder.typicode.com",
      "path": "/posts",
      "query": "userId=1"
    },
    "headers": {
      "User-Agent": [
        "GuzzleHttp/7"
      ],
      "Host": [
        "jsonplaceholder.typicode.com"
      ],
      "Authorization": [
        "Bearer [--REDACTED--]"
      ],
      "Content-Type": [
        "application/json"
      ]
    }
  },
  "level": 100,
  "level_name": "DEBUG",
  "channel": "testing",
  "datetime": "2023-08-16T10:13:41.356030+00:00",
  "extra": {}
}

A Http Response

{
  "message": "Response 0b65fca7-a768-4832-8401-da52aa2885a9",
  "context": {
    "response_id": "0b65fca7-a768-4832-8401-da52aa2885a9",
    "status_code": 200,
    "headers": {
      "Date": ["Wed, 16 Aug 2023 00:41:13 GMT"],
      "Content-Type": ["application/json; charset=utf-8"],
      "Transfer-Encoding": ["chunked"],
      "Connection": ["keep-alive"],
      "X-Powered-By": ["Express"],
      "X-Ratelimit-Limit": ["1000"],
      "X-Ratelimit-Remaining": ["999"],
      "X-Ratelimit-Reset": ["1691921646"],
      "Vary": ["Origin, Accept-Encoding"],
      "Access-Control-Allow-Credentials": ["true"],
      "Cache-Control": ["max-age=43200"],
      "Pragma": ["no-cache"],
      "Expires": ["-1"],
      "X-Content-Type-Options": ["nosniff"],
      "Etag": ["W/\"aa6-j2NSH739l9uq40OywFMn7Y0C/iY\""],
      "Via": ["1.1 vegur"],
      "CF-Cache-Status": ["HIT"],
      "Age": ["18801"],
      "Report-To": [
        "{\"endpoints\":[{\"url\":\"https:\\/\\/a.nel.cloudflare.com\\/report\\/v3?s=gRUkX3pH6GRGwHCE%2BqKF%2ByJRGZs9MkqF8BqXa0nlmYSVzgrcmQkIGfD9lC8IlSXKvSiiyZHxrzgLy8pcOCSMRv5xFh2LyXWOkXDEtFcSr1FINwhjxRwYTZQZIaFzTulP4lUnjlrXdERp57lEXT3C\"}],\"group\":\"cf-nel\",\"max_age\":604800}"
      ],
      "NEL": [
        "{\"success_fraction\":0,\"report_to\":\"cf-nel\",\"max_age\":604800}"
      ],
      "Server": ["cloudflare"],
      "CF-RAY": ["7f75a160dc9991c0-SIN"],
      "alt-svc": ["h3=\":443\"; ma=86400"]
    },
    "body": [
      {
        "userId": 1,
        "id": 1,
        "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
        "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
      }
    ]
  },
  "level": 100,
  "level_name": "DEBUG",
  "channel": "testing",
  "datetime": "2023-08-16T00:41:13.037161+00:00",
  "extra": {}
}

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-08-21