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

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