{"id":15372637,"url":"https://github.com/gmponos/guzzle-log-middleware","last_synced_at":"2025-04-05T09:10:03.657Z","repository":{"id":6580132,"uuid":"55366723","full_name":"gmponos/guzzle-log-middleware","owner":"gmponos","description":"A Guzzle middleware to log request and responses automatically","archived":false,"fork":false,"pushed_at":"2024-07-30T06:03:14.000Z","size":143,"stargazers_count":73,"open_issues_count":9,"forks_count":25,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-29T08:08:39.002Z","etag":null,"topics":["composer","guzzle","guzzle-middleware","guzzlehttp","http","log","logger","logger-middleware","middleware","php","php7","psr-3","rejection"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gmponos.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["gmponos"]}},"created_at":"2016-04-03T20:12:18.000Z","updated_at":"2025-01-31T13:25:49.000Z","dependencies_parsed_at":"2024-10-16T12:40:39.858Z","dependency_job_id":null,"html_url":"https://github.com/gmponos/guzzle-log-middleware","commit_stats":{"total_commits":117,"total_committers":6,"mean_commits":19.5,"dds":"0.042735042735042694","last_synced_commit":"8784459b18f332beb49363a4d71399e784d8c298"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gmponos%2Fguzzle-log-middleware","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gmponos%2Fguzzle-log-middleware/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gmponos%2Fguzzle-log-middleware/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gmponos%2Fguzzle-log-middleware/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gmponos","download_url":"https://codeload.github.com/gmponos/guzzle-log-middleware/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247312084,"owners_count":20918344,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["composer","guzzle","guzzle-middleware","guzzlehttp","http","log","logger","logger-middleware","middleware","php","php7","psr-3","rejection"],"created_at":"2024-10-01T13:52:32.783Z","updated_at":"2025-04-05T09:10:03.575Z","avatar_url":"https://github.com/gmponos.png","language":"PHP","funding_links":["https://github.com/sponsors/gmponos"],"categories":[],"sub_categories":[],"readme":"# Guzzle Log Middleware\n\n[![codecov](https://codecov.io/gh/gmponos/guzzle-log-middleware/branch/master/graph/badge.svg)](https://codecov.io/gh/gmponos/guzzle-log-middleware)\n[![Total Downloads](https://img.shields.io/packagist/dt/gmponos/guzzle_logger.svg)](https://packagist.org/packages/gmponos/guzzle_logger)\n[![Build Status](https://travis-ci.org/gmponos/guzzle-log-middleware.svg?branch=master)](https://travis-ci.org/gmponos/guzzle-log-middleware)\n[![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/gmponos/monolog-slack/blob/master/LICENSE.md)\n\nThis is a middleware for [guzzle](https://github.com/guzzle/guzzle) that will help you automatically log every request \nand response using a PSR-3 logger.\n\nThe middleware is functional with version 6 of Guzzle.\n\n## Install\n\nVia Composer\n\n``` bash\n$ composer require gmponos/guzzle_logger\n```\n\n## Usage\n\n### Simple usage\n\n```php\nuse GuzzleLogMiddleware\\LogMiddleware;\nuse GuzzleHttp\\HandlerStack;\n\n$logger = new Logger();  //A new PSR-3 Logger like Monolog\n$stack = HandlerStack::create(); // will create a stack stack with middlewares of guzzle already pushed inside of it.\n$stack-\u003epush(new LogMiddleware($logger));\n$client = new GuzzleHttp\\Client([\n    'handler' =\u003e $stack,\n]);\n```\n\nFrom now on each request and response you execute using `$client` object will be logged.\nBy default the middleware logs every activity with level `DEBUG`.\n\n### Advanced initialization\n\nThe signature of the `LogMiddleware` class is the following:\n\n```php\n\\GuzzleLogMiddleware\\LogMiddleware(\n    \\Psr\\Log\\LoggerInterface $logger, \n    \\GuzzleLogMiddleware\\Handler\\HandlerInterface $handler = null, \n    bool $onFailureOnly = false, \n    bool $logStatistics = false\n);\n```\n\n- **logger** - The PSR-3 logger to use for logging.\n- **handler** - A HandlerInterface class that will be responsible for logging your request/response. Check Handlers sections.\n- **onFailureOnly** - By default the middleware is set to log every request and response. If you wish to log \nthe HTTP messages only when guzzle returns a rejection set this as true or when an exception occurred. \nGuzzle returns a rejection when [http_errors](http://docs.guzzlephp.org/en/stable/request-options.html#http-errors) option is set to true. \n- **logStatistics** - If you set this option as true then the middleware will also log statistics about the HTTP transaction.\n\n### Handlers\n\nIn order to make the middleware more flexible we allow the developer to initialize it with a handler. \nA handler is the class that will be responsible for logging the HTTP message and it must implement a `HandlerInterface`.\n\nAs an example let's say that we create the following handler:\n\n```php\n\u003c?php\nnamespace GuzzleLogMiddleware\\Handler;\n\nuse Psr\\Http\\Message\\ResponseInterface;\nuse Psr\\Http\\Message\\RequestInterface;\nuse GuzzleHttp\\TransferStats;\nuse Psr\\Log\\LoggerInterface;\n\n/** A simple handler that logs only requests */\nfinal class SimpleHandler implements HandlerInterface\n{\n    public function log(\n        LoggerInterface $logger,\n        RequestInterface $request,\n        ?ResponseInterface $response = null,\n        ?\\Throwable $exception = null,\n        ?TransferStats $stats = null,\n        array $options = []\n    ): void {\n        $logger-\u003edebug('Guzzle HTTP request: ' . \\GuzzleHttp\\Psr7\\str($request));\n        return;\n    }\n}\n```\n\nWe can pass the handler above during construction of the middleware.\n\n```php\n\u003c?php\nuse GuzzleLogMiddleware\\LogMiddleware;\nuse GuzzleHttp\\HandlerStack;\n\n$logger = new Logger();  //A new PSR-3 Logger like Monolog\n$stack = HandlerStack::create(); // will create a stack stack with middlewares of guzzle already pushed inside of it.\n$stack-\u003epush(new LogMiddleware($logger, new SimpleHandler()));\n$client = new GuzzleHttp\\Client([\n    'handler' =\u003e $stack,\n]);\n```\n\nFrom now on all Requests will be logged. Note that at the example above only requests are logged.\n\n**Important**\n\nIf no handler is passed the middleware will initialize it's own handler. At the moment the default one is `MultiRecordArrayHandler`\n\n#### MultiRecordArrayHandler\n\nThis is the default handler used from the middleware. This handler uses internally the `FixedStrategy` and logs all request\nand responses with level debug. This handler adds a separate log entry for each `Request`, `Response`, `Exception` or `TransferStats`.\nThe information about each object are added as a `context` array to the log entry.\n\n#### StringHandler\n\nThis handler uses internally the `FixedStrategy` and logs all request and responses with level debug. You can initialize this handler\nwith a custom strategy. This handler adds a separate log entry for each Request, Response, Exception or TransferStats.\nThe handler converts the objects to strings and the information about each object are added to the `message` of the log entry.\n\n### Log Level Strategies\n\nStrategies are used to define the LogLevel that the handler will use to log each object.\n\n#### FixedStrategy\n\nYou can use this strategy to log each HTTP Message with a specific level.\n\n#### StatusCodeStrategy\n\nYou can use this strategy to log each HTTP Response with a specific level depending on the status code of the Response.\n\n```php\n$strategy = new StatusCodeStrategy(\n    LogLevel::INFO, // Default level used for requests or for responses that status code are not set with a different level.\n    LogLevel::CRITICAL // Default level used for exceptions.\n);\n$strategy-\u003esetLevel(404, LogLevel::WARNING);\n$multiRecordArrayHandler = new MultiRecordArrayHandler($strategy);\n\n$logger = new Logger();  //A new PSR-3 Logger like Monolog\n$stack = HandlerStack::create(); // will create a stack stack with middlewares of guzzle already pushed inside of it.\n$stack-\u003epush(new LogMiddleware($logger, $multiRecordArrayHandler));\n$client = new GuzzleHttp\\Client([\n    'handler' =\u003e $stack,\n]);\n```\n\n### Using options on each request\n\nYou can set on each request options about your log.\n \n ```php\n $client-\u003eget('/', [\n     'log' =\u003e [\n         'on_exception_only' =\u003e true,\n         'statistics' =\u003e true,\n     ]\n ]);\n ```\n\n- ``on_exception_only`` Do not log anything unless if the response status code is above the threshold.\n- ``statistics`` if the `on_exception_only` option/variable is true and this is also true the middleware will log statistics about the HTTP call.\n\n## Change log\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.\n\n## Testing\n\n``` bash\n$ composer test\n```\n\n## Credits\n\n- [George Mponos](gmponos@gmail.com)\n- [Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgmponos%2Fguzzle-log-middleware","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgmponos%2Fguzzle-log-middleware","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgmponos%2Fguzzle-log-middleware/lists"}