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

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