LoggerFactory.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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\Logger;
  12. use Hyperf\Collection\Arr;
  13. use Hyperf\Contract\ConfigInterface;
  14. use Hyperf\Logger\Exception\InvalidConfigException;
  15. use Monolog\Formatter\FormatterInterface;
  16. use Monolog\Formatter\LineFormatter;
  17. use Monolog\Handler\FormattableHandlerInterface;
  18. use Monolog\Handler\HandlerInterface;
  19. use Monolog\Handler\StreamHandler;
  20. use Psr\Container\ContainerInterface;
  21. use Psr\Log\LoggerInterface;
  22. use function Hyperf\Support\make;
  23. class LoggerFactory
  24. {
  25. /**
  26. * @var LoggerInterface[]
  27. */
  28. protected array $loggers = [];
  29. public function __construct(protected ContainerInterface $container, protected ConfigInterface $config)
  30. {
  31. }
  32. public function make($name = 'hyperf', $group = 'default'): LoggerInterface
  33. {
  34. $config = $this->config->get('logger');
  35. if (! isset($config[$group])) {
  36. throw new InvalidConfigException(sprintf('Logger config[%s] is not defined.', $group));
  37. }
  38. $config = $config[$group];
  39. if (is_callable($config)) {
  40. $config = $config($name);
  41. }
  42. $handlers = $this->handlers($config);
  43. $processors = $this->processors($config);
  44. return make(Logger::class, [
  45. 'name' => $name,
  46. 'handlers' => $handlers,
  47. 'processors' => $processors,
  48. ]);
  49. }
  50. public function get($name = 'hyperf', $group = 'default'): LoggerInterface
  51. {
  52. if (isset($this->loggers[$group][$name]) && $this->loggers[$group][$name] instanceof Logger) {
  53. return $this->loggers[$group][$name];
  54. }
  55. return $this->loggers[$group][$name] = $this->make($name, $group);
  56. }
  57. protected function getDefaultFormatterConfig($config)
  58. {
  59. $formatterClass = Arr::get($config, 'formatter.class', LineFormatter::class);
  60. $formatterConstructor = Arr::get($config, 'formatter.constructor', []);
  61. return [
  62. 'class' => $formatterClass,
  63. 'constructor' => $formatterConstructor,
  64. ];
  65. }
  66. protected function getDefaultHandlerConfig($config)
  67. {
  68. $handlerClass = Arr::get($config, 'handler.class', StreamHandler::class);
  69. $handlerConstructor = Arr::get($config, 'handler.constructor', [
  70. 'stream' => BASE_PATH . '/runtime/logs/hyperf.log',
  71. 'level' => Logger::DEBUG,
  72. ]);
  73. return [
  74. 'class' => $handlerClass,
  75. 'constructor' => $handlerConstructor,
  76. ];
  77. }
  78. protected function processors(array $config): array
  79. {
  80. $result = [];
  81. if (! isset($config['processors']) && isset($config['processor'])) {
  82. $config['processors'] = [$config['processor']];
  83. }
  84. foreach ($config['processors'] ?? [] as $value) {
  85. if (is_array($value) && isset($value['class'])) {
  86. $value = make($value['class'], $value['constructor'] ?? []);
  87. }
  88. $result[] = $value;
  89. }
  90. return $result;
  91. }
  92. protected function handlers(array $config): array
  93. {
  94. $handlerConfigs = $config['handlers'] ?? [[]];
  95. $handlers = [];
  96. $defaultHandlerConfig = $this->getDefaultHandlerConfig($config);
  97. $defaultFormatterConfig = $this->getDefaultFormatterConfig($config);
  98. foreach ($handlerConfigs as $value) {
  99. if (is_string($value)) {
  100. if (! $this->config->has($group = 'logger.' . $value)) {
  101. continue;
  102. }
  103. $value = $this->config->get($group . '.handler', []);
  104. if ($this->config->has($group . '.formatter')) {
  105. $value['formatter'] = $this->config->get($group . '.formatter', []);
  106. }
  107. }
  108. $class = $value['class'] ?? $defaultHandlerConfig['class'];
  109. $constructor = $value['constructor'] ?? $defaultHandlerConfig['constructor'];
  110. if (isset($value['formatter'])) {
  111. if (! isset($value['formatter']['constructor'])) {
  112. $value['formatter']['constructor'] = $defaultFormatterConfig['constructor'];
  113. }
  114. }
  115. $formatterConfig = $value['formatter'] ?? $defaultFormatterConfig;
  116. $handlers[] = $this->handler($class, $constructor, $formatterConfig);
  117. }
  118. return $handlers;
  119. }
  120. /**
  121. * @param class-string<HandlerInterface> $class
  122. * @param array $constructor
  123. * @param array $formatterConfig
  124. */
  125. protected function handler($class, $constructor, $formatterConfig): HandlerInterface
  126. {
  127. $handler = make($class, $constructor);
  128. if ($handler instanceof FormattableHandlerInterface) {
  129. $formatterClass = $formatterConfig['class'];
  130. $formatterConstructor = $formatterConfig['constructor'];
  131. /** @var FormatterInterface $formatter */
  132. $formatter = make($formatterClass, $formatterConstructor);
  133. $handler->setFormatter($formatter);
  134. }
  135. return $handler;
  136. }
  137. }