Skip to content

Commit

Permalink
Merge pull request #3 from codex-team/feature/improve-error-context-l…
Browse files Browse the repository at this point in the history
…ogging

Enhance error logging with additional request context for better debugging
  • Loading branch information
pavelzotikov authored Sep 23, 2024
2 parents 40415b3 + 73f0374 commit 48cc957
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
# PhpStorm
/.idea

# Composer lock file
/composer.lock

###> friendsofphp/php-cs-fixer ###
/.php-cs-fixer.cache
###< friendsofphp/php-cs-fixer ###
11 changes: 9 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
Expand All @@ -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
}
}
}
1 change: 1 addition & 0 deletions src/DependencyInjection/HawkExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
30 changes: 29 additions & 1 deletion src/Monolog/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
];
}
}

0 comments on commit 48cc957

Please sign in to comment.