|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Bridge\Monolog\Command; |
| 13 | + |
| 14 | +use Monolog\Formatter\FormatterInterface; |
| 15 | +use Monolog\Logger; |
| 16 | +use Symfony\Bridge\Monolog\Formatter\ConsoleFormatter; |
| 17 | +use Symfony\Bridge\Monolog\Handler\ConsoleHandler; |
| 18 | +use Symfony\Component\Console\Command\Command; |
| 19 | +use Symfony\Component\Console\Exception\LogicException; |
| 20 | +use Symfony\Component\Console\Exception\RuntimeException; |
| 21 | +use Symfony\Component\Console\Input\InputInterface; |
| 22 | +use Symfony\Component\Console\Input\InputOption; |
| 23 | +use Symfony\Component\Console\Output\OutputInterface; |
| 24 | +use Symfony\Component\ExpressionLanguage\ExpressionLanguage; |
| 25 | + |
| 26 | +/** |
| 27 | + * @author Grégoire Pineau <lyrixx@lyrixx.info> |
| 28 | + */ |
| 29 | +class ServerLogCommand extends Command |
| 30 | +{ |
| 31 | + private static $bgColor = ['black', 'blue', 'cyan', 'green', 'magenta', 'red', 'white', 'yellow']; |
| 32 | + |
| 33 | + private $el; |
| 34 | + private $handler; |
| 35 | + |
| 36 | + protected static $defaultName = 'server:log'; |
| 37 | + |
| 38 | + public function isEnabled() |
| 39 | + { |
| 40 | + if (!class_exists(ConsoleFormatter::class)) { |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + // based on a symfony/symfony package, it crashes due a missing FormatterInterface from monolog/monolog |
| 45 | + if (!interface_exists(FormatterInterface::class)) { |
| 46 | + return false; |
| 47 | + } |
| 48 | + |
| 49 | + return parent::isEnabled(); |
| 50 | + } |
| 51 | + |
| 52 | + protected function configure() |
| 53 | + { |
| 54 | + if (!class_exists(ConsoleFormatter::class)) { |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + $this |
| 59 | + ->addOption('host', null, InputOption::VALUE_REQUIRED, 'The server host', '0.0.0
A93C
.0:9911') |
| 60 | + ->addOption('format', null, InputOption::VALUE_REQUIRED, 'The line format', ConsoleFormatter::SIMPLE_FORMAT) |
| 61 | + ->addOption('date-format', null, InputOption::VALUE_REQUIRED, 'The date format', ConsoleFormatter::SIMPLE_DATE) |
| 62 | + ->addOption('filter', null, InputOption::VALUE_REQUIRED, 'An expression to filter log. Example: "level > 200 or channel in [\'app\', \'doctrine\']"') |
| 63 | + ->setDescription('Starts a log server that displays logs in real time') |
| 64 | + ->setHelp(<<<'EOF' |
| 65 | +<info>%command.name%</info> starts a log server to display in real time the log |
| 66 | +messages generated by your application: |
| 67 | +
|
| 68 | + <info>php %command.full_name%</info> |
| 69 | +
|
| 70 | +To get the information as a machine readable format, use the |
| 71 | +<comment>--filter</> option: |
| 72 | +
|
| 73 | +<info>php %command.full_name% --filter=port</info> |
| 74 | +EOF |
| 75 | + ) |
| 76 | + ; |
| 77 | + } |
| 78 | + |
| 79 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 80 | + { |
| 81 | + $filter = $input->getOption('filter'); |
| 82 | + if ($filter) { |
| 83 | + if (!class_exists(ExpressionLanguage::class)) { |
| 84 | + throw new LogicException('Package "symfony/expression-language" is required to use the "filter" option.'); |
| 85 | + } |
| 86 | + $this->el = new ExpressionLanguage(); |
| 87 | + } |
| 88 | + |
| 89 | + $this->handler = new ConsoleHandler($output, true, [ |
| 90 | + OutputInterface::VERBOSITY_NORMAL => Logger::DEBUG, |
| 91 | + ]); |
| 92 | + |
| 93 | + $this->handler->setFormatter(new ConsoleFormatter([ |
| 94 | + 'format' => str_replace('\n', "\n", $input->getOption('format')), |
| 95 | + 'date_format' => $input->getOption('date-format'), |
| 96 | + 'colors' => $output->isDecorated(), |
| 97 | + 'multiline' => OutputInterface::VERBOSITY_DEBUG <= $output->getVerbosity(), |
| 98 | + ])); |
| 99 | + |
| 100 | + if (false === strpos($host = $input->getOption('host'), '://')) { |
| 101 | + $host = 'tcp://'.$host; |
| 102 | + } |
| 103 | + |
| 104 | + if (!$socket = stream_socket_server($host, $errno, $errstr)) { |
| 105 | + throw new RuntimeException(sprintf('Server start failed on "%s": %s %s.', $host, $errstr, $errno)); |
| 106 | + } |
| 107 | + |
| 108 | + foreach ($this->getLogs($socket) as $clientId => $message) { |
| 109 | + $record = unserialize(base64_decode($message)); |
| 110 | + |
| 111 | + // Impossible to decode the message, give up. |
| 112 | + if (false === $record) { |
| 113 | + continue; |
| 114 | + } |
| 115 | + |
| 116 | + if ($filter && !$this->el->evaluate($filter, $record)) { |
| 117 | + continue; |
| 118 | + } |
| 119 | + |
| 120 | + $this->displayLog($output, $clientId, $record); |
| 121 | + } |
| 122 | + |
| 123 | + return 0; |
| 124 | + } |
| 125 | + |
| 126 | + private function getLogs($socket): iterable |
| 127 | + { |
| 128 | + $sockets = [(int) $socket => $socket]; |
| 129 | + $write = []; |
| 130 | + |
| 131 | + while (true) { |
| 132 | + $read = $sockets; |
| 133 | + stream_select($read, $write, $write, null); |
| 134 | + |
| 135 | + foreach ($read as $stream) { |
| 136 | + if ($socket === $stream) { |
| 137 | + $stream = stream_socket_accept($socket); |
| 138 | + $sockets[(int) $stream] = $stream; |
| 139 | + } elseif (feof($stream)) { |
| 140 | + unset($sockets[(int) $stream]); |
| 141 | + fclose($stream); |
| 142 | + } else { |
| 143 | + yield (int) $stream => fgets($stream); |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + private function displayLog(OutputInterface $output, int $clientId, array $record) |
| 150 | + { |
| 151 | + if (isset($record['log_id'])) { |
| 152 | + $clientId = unpack('H*', $record['log_id'])[1]; |
| 153 | + } |
| 154 | + $logBlock = sprintf('<bg=%s> </>', self::$bgColor[$clientId % 8]); |
| 155 | + $output->write($logBlock); |
| 156 | + |
| 157 | + $this->handler->handle($record); |
| 158 | + } |
| 159 | +} |
0 commit comments