Add Monolog support in OutputInterface

2.x 1.3.0
Julien Rosset 2 years ago
parent 29f429d458
commit 183e7ddd24

@ -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": {

@ -0,0 +1,25 @@
<?php
namespace jrosset\CliProgram\Monolog;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Formatter\OutputFormatterInterface;
use Symfony\Component\Console\Output\ConsoleOutput;
/**
* A {@see ConsoleOutput} with a {@see LoggerInterface Monolog Logger}
*/
class ConsoleOutputWithMonolog extends ConsoleOutput {
use TOutputInterfaceWithMonolog;
/**
* @param LoggerInterface $logger The Monolog Logger
* @param int $verbosity The verbosity level (one of the VERBOSITY constants in OutputInterface)
* @param bool|null $decorated Whether to decorate messages (null for auto-guessing)
* @param OutputFormatterInterface|null $formatter Output formatter instance (null to use default OutputFormatter)
*/
public function __construct (LoggerInterface $logger, int $verbosity = self::VERBOSITY_NORMAL, bool $decorated = null, OutputFormatterInterface $formatter = null) {
parent::__construct($verbosity, $decorated, $formatter);
$this->setLogger($logger);
}
}

@ -0,0 +1,76 @@
<?php
namespace jrosset\CliProgram\Monolog;
use Monolog\Logger;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Provide a {@see LoggerInterface Monolog Logger} for {@see OutputInterface}
*/
trait TOutputInterfaceWithMonolog {
/**
* @var LoggerInterface|null The logger
*/
private ?LoggerInterface $logger;
/**
* The logger
*
* @return LoggerInterface|null The logger
*/
public function getLogger (): ?LoggerInterface {
return $this->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);
}
}

1
tests/.gitignore vendored

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

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

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

Loading…
Cancel
Save