vendor/symfony/symfony/src/Symfony/Bridge/Monolog/Handler/ConsoleHandler.php line 111

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 = array(
  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.     /**
  50.      * @param OutputInterface|null $output            The console output to use (the handler remains disabled when passing null
  51.      *                                                until the output is set, e.g. by using console events)
  52.      * @param bool                 $bubble            Whether the messages that are handled can bubble up the stack
  53.      * @param array                $verbosityLevelMap Array that maps the OutputInterface verbosity to a minimum logging
  54.      *                                                level (leave empty to use the default mapping)
  55.      */
  56.     public function __construct(OutputInterface $output null$bubble true, array $verbosityLevelMap = array())
  57.     {
  58.         parent::__construct(Logger::DEBUG$bubble);
  59.         $this->output $output;
  60.         if ($verbosityLevelMap) {
  61.             $this->verbosityLevelMap $verbosityLevelMap;
  62.         }
  63.     }
  64.     /**
  65.      * {@inheritdoc}
  66.      */
  67.     public function isHandling(array $record)
  68.     {
  69.         return $this->updateLevel() && parent::isHandling($record);
  70.     }
  71.     /**
  72.      * {@inheritdoc}
  73.      */
  74.     public function handle(array $record)
  75.     {
  76.         // we have to update the logging level each time because the verbosity of the
  77.         // console output might have changed in the meantime (it is not immutable)
  78.         return $this->updateLevel() && parent::handle($record);
  79.     }
  80.     /**
  81.      * Sets the console output to use for printing logs.
  82.      */
  83.     public function setOutput(OutputInterface $output)
  84.     {
  85.         $this->output $output;
  86.     }
  87.     /**
  88.      * Disables the output.
  89.      */
  90.     public function close()
  91.     {
  92.         $this->output null;
  93.         parent::close();
  94.     }
  95.     /**
  96.      * Before a command is executed, the handler gets activated and the console output
  97.      * is set in order to know where to write the logs.
  98.      */
  99.     public function onCommand(ConsoleCommandEvent $event)
  100.     {
  101.         $output $event->getOutput();
  102.         if ($output instanceof ConsoleOutputInterface) {
  103.             $output $output->getErrorOutput();
  104.         }
  105.         $this->setOutput($output);
  106.     }
  107.     /**
  108.      * After a command has been executed, it disables the output.
  109.      */
  110.     public function onTerminate(ConsoleTerminateEvent $event)
  111.     {
  112.         $this->close();
  113.     }
  114.     /**
  115.      * {@inheritdoc}
  116.      */
  117.     public static function getSubscribedEvents()
  118.     {
  119.         return array(
  120.             ConsoleEvents::COMMAND => array('onCommand'255),
  121.             ConsoleEvents::TERMINATE => array('onTerminate', -255),
  122.         );
  123.     }
  124.     /**
  125.      * {@inheritdoc}
  126.      */
  127.     protected function write(array $record)
  128.     {
  129.         // at this point we've determined for sure that we want to output the record, so use the output's own verbosity
  130.         $this->output->write((string) $record['formatted'], false$this->output->getVerbosity());
  131.     }
  132.     /**
  133.      * {@inheritdoc}
  134.      */
  135.     protected function getDefaultFormatter()
  136.     {
  137.         if (!class_exists(CliDumper::class)) {
  138.             return new LineFormatter();
  139.         }
  140.         if (!$this->output) {
  141.             return new ConsoleFormatter();
  142.         }
  143.         return new ConsoleFormatter(array(
  144.             'colors' => $this->output->isDecorated(),
  145.             'multiline' => OutputInterface::VERBOSITY_DEBUG <= $this->output->getVerbosity(),
  146.         ));
  147.     }
  148.     /**
  149.      * Updates the logging level based on the verbosity setting of the console output.
  150.      *
  151.      * @return bool Whether the handler is enabled and verbosity is not set to quiet
  152.      */
  153.     private function updateLevel()
  154.     {
  155.         if (null === $this->output) {
  156.             return false;
  157.         }
  158.         $verbosity $this->output->getVerbosity();
  159.         if (isset($this->verbosityLevelMap[$verbosity])) {
  160.             $this->setLevel($this->verbosityLevelMap[$verbosity]);
  161.         } else {
  162.             $this->setLevel(Logger::DEBUG);
  163.         }
  164.         return true;
  165.     }
  166. }