diff --git a/CHANGELOG.md b/CHANGELOG.md index da6b2da0..62e1dfb6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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) diff --git a/src/ErrorListener.php b/src/ErrorListener.php index 8ef9deb1..3dbee259 100644 --- a/src/ErrorListener.php +++ b/src/ErrorListener.php @@ -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]);