parent
29f429d458
commit
183e7ddd24
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1 @@
|
|||||||
|
run.log
|
Loading…
Reference in New Issue