ConsoleLogger.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Framework\Logger;
  12. use Hyperf\Contract\StdoutLoggerInterface;
  13. use Symfony\Component\Console\Logger\ConsoleLogger as SymfonyConsoleLogger;
  14. use Symfony\Component\Console\Output\ConsoleOutput;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. class ConsoleLogger extends SymfonyConsoleLogger implements StdoutLoggerInterface
  17. {
  18. public function __construct(?OutputInterface $output = null, array $verbosityLevelMap = [], array $formatLevelMap = [])
  19. {
  20. $output = $output ?? new ConsoleOutput(
  21. (function (): int {
  22. $argv = $_SERVER['argv'] ?? [];
  23. $argv = is_string($argv) ? explode(' ', $argv) : $argv;
  24. return match (true) {
  25. in_array('--quiet', $argv), in_array('-q', $argv) => OutputInterface::VERBOSITY_QUIET,
  26. in_array('-vvv', $argv) => OutputInterface::VERBOSITY_DEBUG,
  27. in_array('-vv', $argv) => OutputInterface::VERBOSITY_VERY_VERBOSE,
  28. in_array('-v', $argv) => OutputInterface::VERBOSITY_VERBOSE,
  29. default => match ((int) getenv('SHELL_VERBOSITY')) {
  30. -1 => OutputInterface::VERBOSITY_QUIET,
  31. 1 => OutputInterface::VERBOSITY_VERBOSE,
  32. 2 => OutputInterface::VERBOSITY_VERY_VERBOSE,
  33. 3 => OutputInterface::VERBOSITY_DEBUG,
  34. default => OutputInterface::VERBOSITY_NORMAL,
  35. },
  36. };
  37. })()
  38. );
  39. parent::__construct($output, $verbosityLevelMap, $formatLevelMap);
  40. }
  41. }