diff --git a/composer.json b/composer.json index 4a3beb5..ad4e770 100644 --- a/composer.json +++ b/composer.json @@ -4,12 +4,12 @@ "keywords": [ ], "minimum-stability": "stable", - "require": { - "php": "^7.4 || ^8.0", - "symfony/console": "^5.4 || ^6.0", - "jrosset/betterphptoken": "^1.0", - "jrosset/collections": "^2.3", - "monolog/monolog": "^2.9 || ^3.0" + "require": { + "php": "^7.4 || ^8.0", + "symfony/console": "^5.4 || ^6.0", + "jrosset/betterphptoken": "^1.0", + "jrosset/collections": "^2.3", + "jrosset/extendedmonolog": "^1.0" }, "autoload": { "psr-4": { diff --git a/src/CliProgram/ApplicationWithCommandMonolog.php b/src/CliProgram/ApplicationWithCommandMonolog.php new file mode 100644 index 0000000..cf291cc --- /dev/null +++ b/src/CliProgram/ApplicationWithCommandMonolog.php @@ -0,0 +1,72 @@ +setLogMainDirectory($logMainDirectory); + } + + /** + * @inheritDoc + */ + protected function getOutputInterfaceForCommand (Command $command, InputInterface $input, OutputInterface $output): OutputInterface { + return new ConsoleOutputWithMonolog( + new ExceptionLogger( + $command->getName(), + [ + new LogDirectoryHandler( + $this->getLogMainDirectory() . DIRECTORY_SEPARATOR + . str_replace(':', DIRECTORY_SEPARATOR, $command->getName()) + ), + ] + ), + $output->getVerbosity(), + $output->isDecorated(), + $output->getFormatter() + ); + } + + /** + * The main log directory for Monolog: on subdirectory by command + * + * @return string The main log directory for Monolog: on subdirectory by command + */ + public function getLogMainDirectory (): string { + return $this->logMainDirectory; + } + /** + * Set the main log directory for Monolog: on subdirectory by command + * + * @param string $logMainDirectory The main log directory for Monolog: on subdirectory by command + * + * @return $this + */ + public function setLogMainDirectory (string $logMainDirectory): self { + $this->logMainDirectory = $logMainDirectory; + return $this; + } +} \ No newline at end of file diff --git a/src/CliProgram/ApplicationWithCommandOutputInterface.php b/src/CliProgram/ApplicationWithCommandOutputInterface.php new file mode 100644 index 0000000..d0338a1 --- /dev/null +++ b/src/CliProgram/ApplicationWithCommandOutputInterface.php @@ -0,0 +1,39 @@ +getOutputInterfaceForCommand($command, $input, $output)); + } +} \ No newline at end of file diff --git a/tests/.gitignore b/tests/.gitignore deleted file mode 100644 index 6579751..0000000 --- a/tests/.gitignore +++ /dev/null @@ -1 +0,0 @@ -run.log \ No newline at end of file diff --git a/tests/Application.php b/tests/Application.php index 76588da..16748cf 100644 --- a/tests/Application.php +++ b/tests/Application.php @@ -2,20 +2,16 @@ namespace jrosset\Tests; +use jrosset\CliProgram\ApplicationWithCommandMonolog; 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 { +class Application extends ApplicationWithCommandMonolog { use TAutoDiscoveryApplication; public function __construct (string $name = 'UNKNOWN', string $version = 'UNKNOWN') { - parent::__construct($name, $version); + parent::__construct(__DIR__ . '/logs/', $name, $version); $spot = new AutoDiscoveryDirectory(__DIR__ . '/Commands'); $spot->getAutoPrefixManagers()->prepend(new AutoPrefixNamespaceManager('\\jrosset\\Tests\\Commands', 'test')); @@ -23,11 +19,4 @@ 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/logs/.gitignore b/tests/logs/.gitignore new file mode 100644 index 0000000..c96a04f --- /dev/null +++ b/tests/logs/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file