diff --git a/.gitignore b/.gitignore index 92e81b9..6a9dadc 100644 --- a/.gitignore +++ b/.gitignore @@ -54,6 +54,9 @@ # PhpStorm /.idea +# Composer lock file +/composer.lock + ###> friendsofphp/php-cs-fixer ### /.php-cs-fixer.cache ###< friendsofphp/php-cs-fixer ### diff --git a/composer.json b/composer.json index 957e45d..de78f50 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "description": "Symfony errors Catcher module for Hawk.so", "keywords": ["hawk", "php", "error", "catcher", "monolog", "symfony"], "type": "library", - "version": "0.0.2", + "version": "0.0.3", "license": "MIT", "require": { "php": "^7.2 || ^8.0", @@ -13,7 +13,9 @@ "jean85/pretty-package-versions": "^1.5 || ^2.0", "symfony/config": "^4.4.20||^5.0.11||^6.0||^7.0", "symfony/dependency-injection": "^4.4.20||^5.0.11||^6.0||^7.0", - "symfony/http-kernel": "^4.4.20||^5.0.11||^6.0||^7.0" + "symfony/http-kernel": "^4.4.20||^5.0.11||^6.0||^7.0", + "php-http/discovery": "^1.15", + "symfony/psr-http-message-bridge": "^1.2||^2.0||^6.4||^7.0" }, "require-dev": { "friendsofphp/php-cs-fixer": "^2.19||^3.40" @@ -30,5 +32,10 @@ }, "scripts": { "csfix": "vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.php --using-cache=no --verbose" + }, + "config": { + "allow-plugins": { + "php-http/discovery": false + } } } diff --git a/src/DependencyInjection/HawkExtension.php b/src/DependencyInjection/HawkExtension.php index 01f6445..78ee5af 100644 --- a/src/DependencyInjection/HawkExtension.php +++ b/src/DependencyInjection/HawkExtension.php @@ -42,6 +42,7 @@ public function load(array $configs, ContainerBuilder $container) // Register Monolog\Handler $container->register(Handler::class) ->setArgument('$catcher', new Reference(Catcher::class)) + ->setArgument('$request', new Reference('request_stack')) ->setArgument('$level', Logger::ERROR) ->addTag('monolog.handler'); } diff --git a/src/Monolog/Handler.php b/src/Monolog/Handler.php index e4dee5a..b9f531f 100644 --- a/src/Monolog/Handler.php +++ b/src/Monolog/Handler.php @@ -5,21 +5,26 @@ namespace HawkBundle\Monolog; use HawkBundle\Catcher; +use Http\Discovery\Psr17FactoryDiscovery; use Monolog\Formatter\LineFormatter; use Monolog\Handler\AbstractProcessingHandler; use Monolog\Logger; +use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory; +use Symfony\Component\HttpFoundation\RequestStack; final class Handler extends AbstractProcessingHandler { use HandlerTrait; private $catcher; + private $request; - public function __construct(Catcher $catcher, $level = Logger::ERROR, bool $bubble = true) + public function __construct(Catcher $catcher, RequestStack $request, $level = Logger::ERROR, bool $bubble = true) { parent::__construct($level, $bubble); $this->catcher = $catcher; + $this->request = $request; } public function doWrite($record): void @@ -29,10 +34,33 @@ public function doWrite($record): void 'title' => (new LineFormatter('%message%'))->format($record) ]; + $data['context'] = $this->collectRequestInfo(); + if (isset($record['context']['exception']) && $record['context']['exception'] instanceof \Throwable) { $data['exception'] = $record['context']['exception']; } $this->catcher->sendEvent($data); } + + private function collectRequestInfo(): array + { + $factory = new PsrHttpFactory( + Psr17FactoryDiscovery::findServerRequestFactory(), + Psr17FactoryDiscovery::findStreamFactory(), + Psr17FactoryDiscovery::findUploadedFileFactory(), + Psr17FactoryDiscovery::findResponseFactory() + ); + + $request = $factory->createRequest( + $this->request->getCurrentRequest() + ); + + return [ + 'method' => $request->getMethod(), + 'uri' => (string) $request->getUri(), + 'query_params' => $request->getQueryParams(), + 'parsed_body' => $request->getParsedBody() + ]; + } }