diff --git a/composer.json b/composer.json index f0be500..4a3beb5 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,8 @@ "php": "^7.4 || ^8.0", "symfony/console": "^5.4 || ^6.0", "jrosset/betterphptoken": "^1.0", - "jrosset/collections": "^2.3" + "jrosset/collections": "^2.3", + "monolog/monolog": "^2.9 || ^3.0" }, "autoload": { "psr-4": { diff --git a/src/CliProgram/Monolog/ConsoleOutputWithMonolog.php b/src/CliProgram/Monolog/ConsoleOutputWithMonolog.php new file mode 100644 index 0000000..de7bbea --- /dev/null +++ b/src/CliProgram/Monolog/ConsoleOutputWithMonolog.php @@ -0,0 +1,25 @@ +setLogger($logger); + } +} \ No newline at end of file diff --git a/src/CliProgram/Monolog/TOutputInterfaceWithMonolog.php b/src/CliProgram/Monolog/TOutputInterfaceWithMonolog.php new file mode 100644 index 0000000..2b38455 --- /dev/null +++ b/src/CliProgram/Monolog/TOutputInterfaceWithMonolog.php @@ -0,0 +1,76 @@ +logger; + } + /** + * Set the logger + * + * @param LoggerInterface|null $logger The logger + * + * @return $this + */ + public function setLogger (?LoggerInterface $logger): self { + $this->logger = $logger; + return $this; + } + + /** + * @inheritDoc + */ + public function write($messages, bool $newline = false, int $options = 0): void { + if (!is_iterable($messages)) { + $messages = [$messages]; + } + + if ($this->logger !== null) { + $verbosities = OutputInterface::VERBOSITY_QUIET | OutputInterface::VERBOSITY_NORMAL | OutputInterface::VERBOSITY_VERBOSE | OutputInterface::VERBOSITY_VERY_VERBOSE | OutputInterface::VERBOSITY_DEBUG; + $verbosity = $verbosities & $options ?: OutputInterface::VERBOSITY_NORMAL; + + if ($verbosity <= OutputInterface::VERBOSITY_QUIET) { + $loggerLevel = Logger::ERROR; + } + elseif ($verbosity <= OutputInterface::VERBOSITY_NORMAL) { + $loggerLevel = Logger::NOTICE; + } + elseif ($verbosity <= OutputInterface::VERBOSITY_VERBOSE) { + $loggerLevel = Logger::INFO; + } + elseif ($verbosity <= OutputInterface::VERBOSITY_VERY_VERBOSE) { + $loggerLevel = Logger::INFO; + } + elseif ($verbosity <= OutputInterface::VERBOSITY_DEBUG) { + $loggerLevel = Logger::DEBUG; + } + else { + $loggerLevel = Logger::NOTICE; + } + + foreach ($messages as $message) { + $this->logger->log($loggerLevel, $message); + } + } + + parent::write($messages, $newline, $options); + } +} \ No newline at end of file diff --git a/tests/.gitignore b/tests/.gitignore new file mode 100644 index 0000000..6579751 --- /dev/null +++ b/tests/.gitignore @@ -0,0 +1 @@ +run.log \ No newline at end of file diff --git a/tests/Application.php b/tests/Application.php index 9658070..76588da 100644 --- a/tests/Application.php +++ b/tests/Application.php @@ -5,6 +5,11 @@ namespace jrosset\Tests; use jrosset\CliProgram\AutoDiscovery\AutoDiscoveryDirectory; use jrosset\CliProgram\AutoDiscovery\TAutoDiscoveryApplication; use jrosset\CliProgram\AutoPrefix\AutoPrefixNamespaceManager; +use jrosset\CliProgram\Monolog\ConsoleOutputWithMonolog; +use Monolog\Handler\StreamHandler; +use Monolog\Logger; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; class Application extends \Symfony\Component\Console\Application { use TAutoDiscoveryApplication; @@ -18,4 +23,11 @@ class Application extends \Symfony\Component\Console\Application { $this->getAutoDiscoverySpots()->prepend($spot); $this->addAutoDiscoveredCommands(); } + + public function run (InputInterface $input = null, OutputInterface $output = null): int { + $logger = new Logger(__CLASS__); + $logger->pushHandler(new StreamHandler(__DIR__ . '/run.log', Logger::DEBUG)); + + return parent::run($input, $output ?? new ConsoleOutputWithMonolog($logger)); + } } \ No newline at end of file diff --git a/tests/Commands/Hello.php b/tests/Commands/Hello.php index 449bc5b..acbd3da 100644 --- a/tests/Commands/Hello.php +++ b/tests/Commands/Hello.php @@ -24,6 +24,7 @@ class Hello extends BaseCommand { * @inheritDoc */ protected function execute (InputInterface $input, OutputInterface $output): int { + $output->writeln('Command : ' . __CLASS__, OutputInterface::VERBOSITY_DEBUG); $output->writeln('Hello !'); return Command::SUCCESS; }