LoggerFactory.php 4.7 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\Contract\ConfigInterface;
  13. use Hyperf\Logger\Exception\InvalidConfigException;
  14. use Hyperf\Utils\Arr;
  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. class LoggerFactory
  23. {
  24. /**
  25. * @var ContainerInterface
  26. */
  27. protected $container;
  28. /**
  29. * @var ConfigInterface
  30. */
  31. protected $config;
  32. /**
  33. * @var array
  34. */
  35. protected $loggers;
  36. public function __construct(ContainerInterface $container)
  37. {
  38. $this->container = $container;
  39. $this->config = $container->get(ConfigInterface::class);
  40. }
  41. public function make($name = 'hyperf', $group = 'default'): LoggerInterface
  42. {
  43. $config = $this->config->get('logger');
  44. if (! isset($config[$group])) {
  45. throw new InvalidConfigException(sprintf('Logger config[%s] is not defined.', $name));
  46. }
  47. $config = $config[$group];
  48. $handlers = $this->handlers($config);
  49. $processors = $this->processors($config);
  50. return make(Logger::class, [
  51. 'name' => $name,
  52. 'handlers' => $handlers,
  53. 'processors' => $processors,
  54. ]);
  55. }
  56. public function get($name = 'hyperf', $group = 'default'): LoggerInterface
  57. {
  58. if (isset($this->loggers[$group][$name]) && $this->loggers[$group][$name] instanceof Logger) {
  59. return $this->loggers[$group][$name];
  60. }
  61. return $this->loggers[$group][$name] = $this->make($name, $group);
  62. }
  63. protected function getDefaultFormatterConfig($config)
  64. {
  65. $formatterClass = Arr::get($config, 'formatter.class', LineFormatter::class);
  66. $formatterConstructor = Arr::get($config, 'formatter.constructor', []);
  67. return [
  68. 'class' => $formatterClass,
  69. 'constructor' => $formatterConstructor,
  70. ];
  71. }
  72. protected function getDefaultHandlerConfig($config)
  73. {
  74. $handlerClass = Arr::get($config, 'handler.class', StreamHandler::class);
  75. $handlerConstructor = Arr::get($config, 'handler.constructor', [
  76. 'stream' => BASE_PATH . '/runtime/logs/hyperf.log',
  77. 'level' => Logger::DEBUG,
  78. ]);
  79. return [
  80. 'class' => $handlerClass,
  81. 'constructor' => $handlerConstructor,
  82. ];
  83. }
  84. protected function processors(array $config): array
  85. {
  86. $result = [];
  87. if (! isset($config['processors']) && isset($config['processor'])) {
  88. $config['processors'] = [$config['processor']];
  89. }
  90. foreach ($config['processors'] ?? [] as $value) {
  91. if (is_array($value) && isset($value['class'])) {
  92. $value = make($value['class'], $value['constructor'] ?? []);
  93. }
  94. $result[] = $value;
  95. }
  96. return $result;
  97. }
  98. protected function handlers(array $config): array
  99. {
  100. $handlerConfigs = $config['handlers'] ?? [[]];
  101. $handlers = [];
  102. $defaultHandlerConfig = $this->getDefaultHandlerConfig($config);
  103. $defaultFormatterConfig = $this->getDefaultFormatterConfig($config);
  104. foreach ($handlerConfigs as $value) {
  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. }