Skip to content

Commit

Permalink
Allow to use ErrorListiner without logger + Minor refactoring (#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik authored Feb 8, 2024
1 parent 3ac7fa6 commit 8d5f767
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## 2.1.3 under development

- no changes in this release.
- Enh #194: Allow to use `ErrorListiner` without logger (@vjik)

## 2.1.2 December 26, 2023

Expand All @@ -14,7 +14,7 @@

## 2.1.0 May 28, 2023

- Bug #172: Fix accepting `:` as command name separator, offer using it by default (samdark)
- Bug #172: Fix accepting `:` as command name separator, offer using it by default (@samdark)
- Bug #179: Remove duplicate messages about server address (@samdark)
- Enh #180: Enhance output of `serve` command, add `--xdebug` option for `serve` (@xepozz)

Expand Down
13 changes: 6 additions & 7 deletions src/ErrorListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,26 @@

final class ErrorListener
{
public function __construct(private LoggerInterface $logger)
public function __construct(private ?LoggerInterface $logger = null)
{
}

/**
* @psalm-suppress PossiblyNullArgument
*/
public function onError(ConsoleErrorEvent $event): void
{
if ($this->logger === null) {
return;
}

$exception = $event->getError();
$command = $event->getCommand();

$commandName = ($command !== null && $command->getName() !== null) ? $command->getName() : 'unknown';

$message = sprintf(
'%s: %s in %s:%s while running console command "%s".',
$exception::class,
$exception->getMessage(),
$exception->getFile(),
$exception->getLine(),
$commandName,
$command?->getName() ?? 'unknown',
);

$this->logger->error($message, ['exception' => $exception]);
Expand Down

0 comments on commit 8d5f767

Please sign in to comment.