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

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