├── CHANGELOG.md ├── LICENSE ├── README.md ├── composer.json └── src └── LoggerPlugin.php /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | ## 1.4.0 - 2024-10-31 4 | 5 | - Support PHP 8.3 and 8.4 6 | - Remove support for EOL PHP versions < 8.2 7 | 8 | CI: 9 | 10 | - Rewrote testing with phpunit instead of phpspec. 11 | - Removed unused scrutinizer from build 12 | 13 | ## 1.3.1 - 2024-09-01 14 | 15 | - Removed the last place where we forgot the request in the log context. 16 | 17 | ## 1.3.0 - 2022-02-11 18 | 19 | - Do not log request when loggin response again, but use UID to identify request 20 | that belongs to response. 21 | If you use a logger that does not log `info` severity and want the request 22 | logged when an error happened, use a Fingerscrossed log handler to also log 23 | info if any error is logged. 24 | - Removed the request and response from the log context. They did not get 25 | printed because they don't implement `__toString`. 26 | - Supporting the newly introduced message formatter method 27 | `formatResponseForRequest` that allows more flexibility in the formatter. 28 | See https://github.com/php-http/message/pull/146 29 | 30 | ## 1.2.2 - 2021-07-26 31 | 32 | - Allow installation with psr/log version 2 and 3 33 | 34 | ## 1.2.1 - 2020-11-27 35 | 36 | - Allow installation with PHP 8.0 37 | 38 | ## 1.2.0 - 2020-07-14 39 | 40 | - Use `hrtime` to avoid clock movement bugs 41 | - Support only PHP 7.1-8.0 42 | 43 | ## 1.1.0 - 2019-01-19 44 | 45 | - Support client-common 1.9 and 2 46 | - Measure the time it took to do the request 47 | 48 | ## 1.0.0 - 2016-05-05 49 | 50 | - Initial release 51 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015-2016 PHP HTTP Team 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is furnished 8 | to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Logger Plugin 2 | 3 | [![Latest Version](https://img.shields.io/github/release/php-http/logger-plugin.svg?style=flat-square)](https://github.com/php-http/logger-plugin/releases) 4 | [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE) 5 | [![Build Status](https://img.shields.io/travis/php-http/logger-plugin.svg?style=flat-square)](https://travis-ci.org/php-http/logger-plugin) 6 | [![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/php-http/logger-plugin.svg?style=flat-square)](https://scrutinizer-ci.com/g/php-http/logger-plugin) 7 | [![Quality Score](https://img.shields.io/scrutinizer/g/php-http/logger-plugin.svg?style=flat-square)](https://scrutinizer-ci.com/g/php-http/logger-plugin) 8 | [![Total Downloads](https://img.shields.io/packagist/dt/php-http/logger-plugin.svg?style=flat-square)](https://packagist.org/packages/php-http/logger-plugin) 9 | 10 | **PSR-3 Logger plugin for HTTPlug.** 11 | 12 | 13 | ## Install 14 | 15 | Via Composer 16 | 17 | ``` bash 18 | $ composer require php-http/logger-plugin 19 | ``` 20 | 21 | 22 | ## Documentation 23 | 24 | Please see the [official documentation](http://docs.php-http.org/en/latest/plugins/logger.html). 25 | 26 | 27 | ## Testing 28 | 29 | ``` bash 30 | $ composer test 31 | ``` 32 | 33 | 34 | ## Contributing 35 | 36 | Please see our [contributing guide](http://docs.php-http.org/en/latest/development/contributing.html). 37 | 38 | 39 | ## Security 40 | 41 | If you discover any security related issues, please contact us at [security@php-http.org](mailto:security@php-http.org). 42 | 43 | 44 | ## License 45 | 46 | The MIT License (MIT). Please see [License File](LICENSE) for more information. 47 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "php-http/logger-plugin", 3 | "description": "PSR-3 Logger plugin for HTTPlug", 4 | "license": "MIT", 5 | "keywords": ["logger", "http", "httplug", "plugin"], 6 | "homepage": "http://httplug.io", 7 | "authors": [ 8 | { 9 | "name": "Márk Sági-Kazár", 10 | "email": "mark.sagikazar@gmail.com" 11 | } 12 | ], 13 | "require": { 14 | "php": "^8.2", 15 | "psr/log": "^2 || ^3", 16 | "php-http/client-common": "^2.0", 17 | "php-http/message": "^1.13" 18 | }, 19 | "autoload": { 20 | "psr-4": { 21 | "Http\\Client\\Common\\Plugin\\": "src/" 22 | } 23 | }, 24 | "autoload-dev": { 25 | "psr-4": { 26 | "Http\\Client\\Common\\Plugin\\": "tests/" 27 | } 28 | }, 29 | "scripts": { 30 | "test": "vendor/bin/phpunit" 31 | }, 32 | "require-dev": { 33 | "phpunit/phpunit": "^11.4.3", 34 | "nyholm/psr7": "^1.8" 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/LoggerPlugin.php: -------------------------------------------------------------------------------- 1 | 18 | */ 19 | final readonly class LoggerPlugin implements Plugin 20 | { 21 | private Formatter $formatter; 22 | 23 | public function __construct( 24 | private LoggerInterface $logger, 25 | ?Formatter $formatter = null 26 | ) { 27 | $this->formatter = $formatter ?? new SimpleFormatter(); 28 | } 29 | 30 | public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise 31 | { 32 | $start = hrtime(true) / 1E6; 33 | $uid = uniqid('', true); 34 | $this->logger->info(sprintf("Sending request:\n%s", $this->formatter->formatRequest($request)), ['uid' => $uid]); 35 | 36 | return $next($request)->then(function (ResponseInterface $response) use ($start, $uid, $request) { 37 | $milliseconds = (int) round(hrtime(true) / 1E6 - $start); 38 | $formattedResponse = $this->formatter->formatResponseForRequest($response, $request); 39 | $this->logger->info( 40 | sprintf("Received response:\n%s", $formattedResponse), 41 | [ 42 | 'milliseconds' => $milliseconds, 43 | 'uid' => $uid, 44 | ] 45 | ); 46 | 47 | return $response; 48 | }, function (Exception $exception) use ($request, $start, $uid) { 49 | $milliseconds = (int) round(hrtime(true) / 1E6 - $start); 50 | if ($exception instanceof Exception\HttpException) { 51 | $formattedResponse = $this->formatter->formatResponseForRequest($exception->getResponse(), $exception->getRequest()); 52 | $this->logger->error( 53 | sprintf("Error:\n%s\nwith response:\n%s", $exception->getMessage(), $formattedResponse), 54 | [ 55 | 'exception' => $exception, 56 | 'milliseconds' => $milliseconds, 57 | 'uid' => $uid, 58 | ] 59 | ); 60 | } else { 61 | $this->logger->error( 62 | sprintf("Error:\n%s\nwhen sending request:\n%s", $exception->getMessage(), $this->formatter->formatRequest($request)), 63 | [ 64 | 'exception' => $exception, 65 | 'milliseconds' => $milliseconds, 66 | 'uid' => $uid, 67 | ] 68 | ); 69 | } 70 | 71 | throw $exception; 72 | }); 73 | } 74 | } 75 | --------------------------------------------------------------------------------