vendor/symfony/monolog-bridge/Handler/ConsoleHandler.php line 119

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Bridge\Monolog\Handler;
  11. use Monolog\Formatter\FormatterInterface;
  12. use Monolog\Formatter\LineFormatter;
  13. use Monolog\Handler\AbstractProcessingHandler;
  14. use Monolog\Logger;
  15. use Symfony\Bridge\Monolog\Formatter\ConsoleFormatter;
  16. use Symfony\Component\Console\ConsoleEvents;
  17. use Symfony\Component\Console\Event\ConsoleCommandEvent;
  18. use Symfony\Component\Console\Event\ConsoleTerminateEvent;
  19. use Symfony\Component\Console\Output\ConsoleOutputInterface;
  20. use Symfony\Component\Console\Output\OutputInterface;
  21. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  22. use Symfony\Component\VarDumper\Dumper\CliDumper;
  23. /**
  24.  * Writes logs to the console output depending on its verbosity setting.
  25.  *
  26.  * It is disabled by default and gets activated as soon as a command is executed.
  27.  * Instead of listening to the console events, the output can also be set manually.
  28.  *
  29.  * The minimum logging level at which this handler will be triggered depends on the
  30.  * verbosity setting of the console output. The default mapping is:
  31.  * - OutputInterface::VERBOSITY_NORMAL will show all WARNING and higher logs
  32.  * - OutputInterface::VERBOSITY_VERBOSE (-v) will show all NOTICE and higher logs
  33.  * - OutputInterface::VERBOSITY_VERY_VERBOSE (-vv) will show all INFO and higher logs
  34.  * - OutputInterface::VERBOSITY_DEBUG (-vvv) will show all DEBUG and higher logs, i.e. all logs
  35.  *
  36.  * This mapping can be customized with the $verbosityLevelMap constructor parameter.
  37.  *
  38.  * @author Tobias Schultze <http://tobion.de>
  39.  */
  40. class ConsoleHandler extends AbstractProcessingHandler implements EventSubscriberInterface
  41. {
  42.     private $output;
  43.     private $verbosityLevelMap = [
  44.         OutputInterface::VERBOSITY_QUIET => Logger::ERROR,
  45.         OutputInterface::VERBOSITY_NORMAL => Logger::WARNING,
  46.         OutputInterface::VERBOSITY_VERBOSE => Logger::NOTICE,
  47.         OutputInterface::VERBOSITY_VERY_VERBOSE => Logger::INFO,
  48.         OutputInterface::VERBOSITY_DEBUG => Logger::DEBUG,
  49.     ];
  50.     private $consoleFormatterOptions;
  51.     /**
  52.      * @param OutputInterface|null $output            The console output to use (the handler remains disabled when passing null
  53.      *                                                until the output is set, e.g. by using console events)
  54.      * @param bool                 $bubble            Whether the messages that are handled can bubble up the stack
  55.      * @param array                $verbosityLevelMap Array that maps the OutputInterface verbosity to a minimum logging
  56.      *                                                level (leave empty to use the default mapping)
  57.      */
  58.     public function __construct(OutputInterface $output nullbool $bubble true, array $verbosityLevelMap = [], array $consoleFormatterOptions = [])
  59.     {
  60.         parent::__construct(Logger::DEBUG$bubble);
  61.         $this->output $output;
  62.         if ($verbosityLevelMap) {
  63.             $this->verbosityLevelMap $verbosityLevelMap;
  64.         }
  65.         $this->consoleFormatterOptions $consoleFormatterOptions;
  66.     }
  67.     /**
  68.      * {@inheritdoc}
  69.      *
  70.      * @return bool
  71.      */
  72.     public function isHandling(array $record)
  73.     {
  74.         return $this->updateLevel() && parent::isHandling($record);
  75.     }
  76.     /**
  77.      * {@inheritdoc}
  78.      *
  79.      * @return bool
  80.      */
  81.     public function handle(array $record)
  82.     {
  83.         // we have to update the logging level each time because the verbosity of the
  84.         // console output might have changed in the meantime (it is not immutable)
  85.         return $this->updateLevel() && parent::handle($record);
  86.     }
  87.     /**
  88.      * Sets the console output to use for printing logs.
  89.      */
  90.     public function setOutput(OutputInterface $output)
  91.     {
  92.         $this->output $output;
  93.     }
  94.     /**
  95.      * Disables the output.
  96.      */
  97.     public function close()
  98.     {
  99.         $this->output null;
  100.         parent::close();
  101.     }
  102.     /**
  103.      * Before a command is executed, the handler gets activated and the console output
  104.      * is set in order to know where to write the logs.
  105.      */
  106.     public function onCommand(ConsoleCommandEvent $event)
  107.     {
  108.         $output $event->getOutput();
  109.         if ($output instanceof ConsoleOutputInterface) {
  110.             $output $output->getErrorOutput();
  111.         }
  112.         $this->setOutput($output);
  113.     }
  114.     /**
  115.      * After a command has been executed, it disables the output.
  116.      */
  117.     public function onTerminate(ConsoleTerminateEvent $event)
  118.     {
  119.         $this->close();
  120.     }
  121.     /**
  122.      * {@inheritdoc}
  123.      */
  124.     public static function getSubscribedEvents()
  125.     {
  126.         return [
  127.             ConsoleEvents::COMMAND => ['onCommand'255],
  128.             ConsoleEvents::TERMINATE => ['onTerminate', -255],
  129.         ];
  130.     }
  131.     /**
  132.      * {@inheritdoc}
  133.      *
  134.      * @return void
  135.      */
  136.     protected function write(array $record)
  137.     {
  138.         // at this point we've determined for sure that we want to output the record, so use the output's own verbosity
  139.         $this->output->write((string) $record['formatted'], false$this->output->getVerbosity());
  140.     }
  141.     /**
  142.      * {@inheritdoc}
  143.      *
  144.      * @return FormatterInterface
  145.      */
  146.     protected function getDefaultFormatter()
  147.     {
  148.         if (!class_exists(CliDumper::class)) {
  149.             return new LineFormatter();
  150.         }
  151.         if (!$this->output) {
  152.             return new ConsoleFormatter($this->consoleFormatterOptions);
  153.         }
  154.         return new ConsoleFormatter(array_replace([
  155.             'colors' => $this->output->isDecorated(),
  156.             'multiline' => OutputInterface::VERBOSITY_DEBUG <= $this->output->getVerbosity(),
  157.         ], $this->consoleFormatterOptions));
  158.     }
  159.     /**
  160.      * Updates the logging level based on the verbosity setting of the console output.
  161.      *
  162.      * @return bool Whether the handler is enabled and verbosity is not set to quiet
  163.      */
  164.     private function updateLevel(): bool
  165.     {
  166.         if (null === $this->output) {
  167.             return false;
  168.         }
  169.         $verbosity $this->output->getVerbosity();
  170.         if (isset($this->verbosityLevelMap[$verbosity])) {
  171.             $this->setLevel($this->verbosityLevelMap[$verbosity]);
  172.         } else {
  173.             $this->setLevel(Logger::DEBUG);
  174.         }
  175.         return true;
  176.     }
  177. }