setLogger($logger); } /** * @inheritDoc */ public function getErrorOutput (): self { return new static(CliHelper::getErrorOutput($this->getOutput()), $this->getLogger()); } /** * The logger * * @return TLogger|null The logger */ public function getLogger () { return $this->logger; } /** * Set the logger * * @param TLogger|null $logger The logger * * @return $this * * @throws InvalidArgumentException If the logger is not null or an instance for {@see LoggerInterface} * * @noinspection PhpMissingParamTypeInspection */ public function setLogger ($logger): self { if ($logger !== null && !$logger instanceof LoggerInterface) { throw new InvalidArgumentException('The logger must be null or a ' . LoggerInterface::class . ' instance'); } $this->logger = $logger; return $this; } /** * Write into the logger * * @param iterable|string $messages The messages to write * @param bool $newline Newline at the end ? * @param int $options A bitmask of options * * @return void */ protected function writeToLogger ($messages, bool $newline = false, int $options = 0): void { if ($this->logger === null || ($options & static::OPTION_SKIP_LOGGER) === static::OPTION_SKIP_LOGGER) { return; } //region Calcul log level $verbosities = OutputInterface::VERBOSITY_QUIET | OutputInterface::VERBOSITY_NORMAL | OutputInterface::VERBOSITY_VERBOSE | OutputInterface::VERBOSITY_VERY_VERBOSE | OutputInterface::VERBOSITY_DEBUG; $loggerLevel = $this->getLoggerLevelFromVerbosity( $verbosities & $options ? : OutputInterface::VERBOSITY_NORMAL ); //endregion if (!is_iterable($messages)) { $messages = [$messages]; } foreach ($messages as $message) { $this->getLogger()->log($loggerLevel, strip_tags($message) . ($newline ? PHP_EOL : '')); } } /** * OGet the logger level from verbosity * * @param int $verbosity The verbosity * * @return mixed The logger level * @noinspection PhpReturnDocTypeMismatchInspection */ protected function getLoggerLevelFromVerbosity (int $verbosity) { return $verbosity; } /** * @inheritDoc */ public function write ($messages, bool $newline = false, int $options = 0) { $this->writeToLogger($messages, $newline, $options); return parent::write($messages, $newline, $options); } /** * @inheritDoc */ public function writeLn ($messages, int $options = 0) { $this->writeToLogger($messages, true, $options); return parent::writeLn($messages, $options); } }