LoggerFactory.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. $handlers = $this->handlers($config);
  40. $processors = $this->processors($config);
  41. return make(Logger::class, [
  42. 'name' => $name,
  43. 'handlers' => $handlers,
  44. 'processors' => $processors,
  45. ]);
  46. }
  47. public function get($name = 'hyperf', $group = 'default'): LoggerInterface
  48. {
  49. if (isset($this->loggers[$group][$name]) && $this->loggers[$group][$name] instanceof Logger) {
  50. return $this->loggers[$group][$name];
  51. }
  52. return $this->loggers[$group][$name] = $this->make($name, $group);
  53. }
  54. protected function getDefaultFormatterConfig($config)
  55. {
  56. $formatterClass = Arr::get($config, 'formatter.class', LineFormatter::class);
  57. $formatterConstructor = Arr::get($config, 'formatter.constructor', []);
  58. return [
  59. 'class' => $formatterClass,
  60. 'constructor' => $formatterConstructor,
  61. ];
  62. }
  63. protected function getDefaultHandlerConfig($config)
  64. {
  65. $handlerClass = Arr::get($config, 'handler.class', StreamHandler::class);
  66. $handlerConstructor = Arr::get($config, 'handler.constructor', [
  67. 'stream' => BASE_PATH . '/runtime/logs/hyperf.log',
  68. 'level' => Logger::DEBUG,
  69. ]);
  70. return [
  71. 'class' => $handlerClass,
  72. 'constructor' => $handlerConstructor,
  73. ];
  74. }
  75. protected function processors(array $config): array
  76. {
  77. $result = [];
  78. if (! isset($config['processors']) && isset($config['processor'])) {
  79. $config['processors'] = [$config['processor']];
  80. }
  81. foreach ($config['processors'] ?? [] as $value) {
  82. if (is_array($value) && isset($value['class'])) {
  83. $value = make($value['class'], $value['constructor'] ?? []);
  84. }
  85. $result[] = $value;
  86. }
  87. return $result;
  88. }
  89. protected function handlers(array $config): array
  90. {
  91. $handlerConfigs = $config['handlers'] ?? [[]];
  92. $handlers = [];
  93. $defaultHandlerConfig = $this->getDefaultHandlerConfig($config);
  94. $defaultFormatterConfig = $this->getDefaultFormatterConfig($config);
  95. foreach ($handlerConfigs as $value) {
  96. if (is_string($value)) {
  97. if (! $this->config->has($group = 'logger.' . $value)) {
  98. continue;
  99. }
  100. $value = $this->config->get($group . '.handler', []);
  101. if ($this->config->has($group . '.formatter')) {
  102. $value['formatter'] = $this->config->get($group . '.formatter', []);
  103. }
  104. }
  105. $class = $value['class'] ?? $defaultHandlerConfig['class'];
  106. $constructor = $value['constructor'] ?? $defaultHandlerConfig['constructor'];
  107. if (isset($value['formatter'])) {
  108. if (! isset($value['formatter']['constructor'])) {
  109. $value['formatter']['constructor'] = $defaultFormatterConfig['constructor'];
  110. }
  111. }
  112. $formatterConfig = $value['formatter'] ?? $defaultFormatterConfig;
  113. $handlers[] = $this->handler($class, $constructor, $formatterConfig);
  114. }
  115. return $handlers;
  116. }
  117. protected function handler($class, $constructor, $formatterConfig): HandlerInterface
  118. {
  119. /** @var HandlerInterface $handler */
  120. $handler = make($class, $constructor);
  121. if ($handler instanceof FormattableHandlerInterface) {
  122. $formatterClass = $formatterConfig['class'];
  123. $formatterConstructor = $formatterConfig['constructor'];
  124. /** @var FormatterInterface $formatter */
  125. $formatter = make($formatterClass, $formatterConstructor);
  126. $handler->setFormatter($formatter);
  127. }
  128. return $handler;
  129. }
  130. }