Logger.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. declare(strict_types=1);
  3. namespace Yansongda\Supports;
  4. use Exception;
  5. use Monolog\Formatter\FormatterInterface;
  6. use Monolog\Formatter\LineFormatter;
  7. use Monolog\Handler\AbstractProcessingHandler;
  8. use Monolog\Handler\RotatingFileHandler;
  9. use Monolog\Handler\StreamHandler;
  10. use Monolog\Logger as BaseLogger;
  11. use Psr\Log\LoggerInterface;
  12. /**
  13. * @method static void emergency($message, array $context = [])
  14. * @method static void alert($message, array $context = [])
  15. * @method static void critical($message, array $context = [])
  16. * @method static void error($message, array $context = [])
  17. * @method static void warning($message, array $context = [])
  18. * @method static void notice($message, array $context = [])
  19. * @method static void info($message, array $context = [])
  20. * @method static void debug($message, array $context = [])
  21. * @method static void log($message, array $context = [])
  22. */
  23. class Logger
  24. {
  25. protected ?LoggerInterface $logger = null;
  26. protected ?FormatterInterface $formatter = null;
  27. protected ?AbstractProcessingHandler $handler = null;
  28. protected array $config = [
  29. 'file' => null,
  30. 'identify' => 'yansongda.supports',
  31. 'level' => BaseLogger::DEBUG,
  32. 'type' => 'daily',
  33. 'max_files' => 30,
  34. ];
  35. /**
  36. * Bootstrap.
  37. */
  38. public function __construct(array $config = [])
  39. {
  40. $this->setConfig($config);
  41. }
  42. /**
  43. * Forward call.
  44. *
  45. * @throws Exception
  46. */
  47. public function __call(string $method, array $args): void
  48. {
  49. call_user_func_array([$this->getLogger(), $method], $args);
  50. }
  51. /**
  52. * Set logger.
  53. */
  54. public function setLogger(LoggerInterface $logger): Logger
  55. {
  56. $this->logger = $logger;
  57. return $this;
  58. }
  59. /**
  60. * Return the logger instance.
  61. *
  62. * @throws Exception
  63. */
  64. public function getLogger(): LoggerInterface
  65. {
  66. return $this->logger ??= $this->createLogger();
  67. }
  68. public function createLogger(): BaseLogger
  69. {
  70. $handler = $this->getHandler();
  71. $handler->setFormatter($this->getFormatter());
  72. $logger = new BaseLogger($this->config['identify']);
  73. $logger->pushHandler($handler);
  74. return $logger;
  75. }
  76. /**
  77. * setFormatter.
  78. *
  79. * @return $this
  80. */
  81. public function setFormatter(FormatterInterface $formatter): self
  82. {
  83. $this->formatter = $formatter;
  84. return $this;
  85. }
  86. /**
  87. * getFormatter.
  88. */
  89. public function getFormatter(): FormatterInterface
  90. {
  91. return $this->formatter ??= $this->createFormatter();
  92. }
  93. /**
  94. * createFormatter.
  95. */
  96. public function createFormatter(): LineFormatter
  97. {
  98. return new LineFormatter(
  99. "%datetime% > %channel%.%level_name% > %message% %context% %extra%\n\n",
  100. null,
  101. false,
  102. true
  103. );
  104. }
  105. /**
  106. * setHandler.
  107. *
  108. * @return $this
  109. */
  110. public function setHandler(AbstractProcessingHandler $handler): self
  111. {
  112. $this->handler = $handler;
  113. return $this;
  114. }
  115. public function getHandler(): AbstractProcessingHandler
  116. {
  117. return $this->handler ??= $this->createHandler();
  118. }
  119. public function createHandler(): AbstractProcessingHandler
  120. {
  121. $file = $this->config['file'] ?? sys_get_temp_dir().'/logs/'.$this->config['identify'].'.log';
  122. if ('single' === $this->config['type']) {
  123. return new StreamHandler($file, $this->config['level']);
  124. }
  125. return new RotatingFileHandler($file, $this->config['max_files'], $this->config['level']);
  126. }
  127. /**
  128. * setConfig.
  129. *
  130. * @return $this
  131. */
  132. public function setConfig(array $config): self
  133. {
  134. $this->config = array_merge($this->config, $config);
  135. return $this;
  136. }
  137. /**
  138. * getConfig.
  139. */
  140. public function getConfig(): array
  141. {
  142. return $this->config;
  143. }
  144. }