Add Applications working with Monolog

2.x 1.5.0
Julien Rosset 2 years ago
parent cac7055c9d
commit 94105d829a

@ -9,7 +9,7 @@
"symfony/console": "^5.4 || ^6.0",
"jrosset/betterphptoken": "^1.0",
"jrosset/collections": "^2.3",
"monolog/monolog": "^2.9 || ^3.0"
"jrosset/extendedmonolog": "^1.0"
},
"autoload": {
"psr-4": {

@ -0,0 +1,72 @@
<?php
namespace jrosset\CliProgram;
use jrosset\CliProgram\Monolog\ConsoleOutputWithMonolog;
use jrosset\ExtendedMonolog\ExceptionLogger;
use jrosset\ExtendedMonolog\LogDirectoryHandler;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* An application with a {@see LogDirectoryHandler Monolog log directory} for each command
*/
class ApplicationWithCommandMonolog extends ApplicationWithCommandOutputInterface {
/**
* @var string The main log directory for Monolog: on subdirectory by command
*/
private string $logMainDirectory;
/**
* Initialization
*
* @param string $logMainDirectory The main log directory for Monolog: on subdirectory by command
* @param string $name The application name
* @param string $version The application version
*/
public function __construct (string $logMainDirectory, string $name = 'UNKNOWN', string $version = 'UNKNOWN') {
parent::__construct($name, $version);
$this->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;
}
}

@ -0,0 +1,39 @@
<?php
namespace jrosset\CliProgram;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Throwable;
/**
* An application with a by-command {@see OutputInterface}
*/
class ApplicationWithCommandOutputInterface extends Application {
/**
* Get the {@see OutputInterface} for a command
*
* @param Command $command The command
* @param InputInterface $input The input
* @param OutputInterface $output The existing output
*
* @return OutputInterface The output for the command
*
* @throws Throwable On error
*
* @noinspection PhpUnusedParameterInspection
*/
protected function getOutputInterfaceForCommand (Command $command, InputInterface $input, OutputInterface $output): OutputInterface {
return $output;
}
/**
* @inheritDoc
* @throws Throwable
*/
protected function doRunCommand (Command $command, InputInterface $input, OutputInterface $output): int {
return parent::doRunCommand($command, $input, $this->getOutputInterfaceForCommand($command, $input, $output));
}
}

1
tests/.gitignore vendored

@ -1 +0,0 @@
run.log

@ -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));
}
}

@ -0,0 +1,2 @@
*
!.gitignore
Loading…
Cancel
Save