Logger.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. namespace Yansongda\Supports;
  3. use Exception;
  4. use Monolog\Formatter\FormatterInterface;
  5. use Monolog\Formatter\LineFormatter;
  6. use Monolog\Handler\AbstractHandler;
  7. use Monolog\Handler\RotatingFileHandler;
  8. use Monolog\Handler\StreamHandler;
  9. use Monolog\Logger as BaseLogger;
  10. use Psr\Log\LoggerInterface;
  11. /**
  12. * @method void emergency($message, array $context = array())
  13. * @method void alert($message, array $context = array())
  14. * @method void critical($message, array $context = array())
  15. * @method void error($message, array $context = array())
  16. * @method void warning($message, array $context = array())
  17. * @method void notice($message, array $context = array())
  18. * @method void info($message, array $context = array())
  19. * @method void debug($message, array $context = array())
  20. * @method void log($message, array $context = array())
  21. */
  22. class Logger
  23. {
  24. /**
  25. * Logger instance.
  26. *
  27. * @var LoggerInterface
  28. */
  29. protected $logger;
  30. /**
  31. * formatter.
  32. *
  33. * @var \Monolog\Formatter\FormatterInterface
  34. */
  35. protected $formatter;
  36. /**
  37. * handler.
  38. *
  39. * @var AbstractHandler
  40. */
  41. protected $handler;
  42. /**
  43. * config.
  44. *
  45. * @var array
  46. */
  47. protected $config = [
  48. 'file' => null,
  49. 'identify' => 'yansongda.supports',
  50. 'level' => BaseLogger::DEBUG,
  51. 'type' => 'daily',
  52. 'max_files' => 30,
  53. ];
  54. /**
  55. * Forward call.
  56. *
  57. * @author yansongda <me@yansongda.cn>
  58. *
  59. * @param string $method
  60. * @param array $args
  61. *
  62. * @throws Exception
  63. */
  64. public function __call($method, $args): void
  65. {
  66. call_user_func_array([$this->getLogger(), $method], $args);
  67. }
  68. /**
  69. * Set logger.
  70. *
  71. * @author yansongda <me@yansongda.cn>
  72. */
  73. public function setLogger(LoggerInterface $logger): Logger
  74. {
  75. $this->logger = $logger;
  76. return $this;
  77. }
  78. /**
  79. * Return the logger instance.
  80. *
  81. * @author yansongda <me@yansongda.cn>
  82. *
  83. * @throws Exception
  84. */
  85. public function getLogger(): LoggerInterface
  86. {
  87. if (is_null($this->logger)) {
  88. $this->logger = $this->createLogger();
  89. }
  90. return $this->logger;
  91. }
  92. /**
  93. * Make a default log instance.
  94. *
  95. * @author yansongda <me@yansongda.cn>
  96. *
  97. * @throws Exception
  98. */
  99. public function createLogger(): BaseLogger
  100. {
  101. $handler = $this->getHandler();
  102. $handler->setFormatter($this->getFormatter());
  103. $logger = new BaseLogger($this->config['identify']);
  104. $logger->pushHandler($handler);
  105. return $logger;
  106. }
  107. /**
  108. * setFormatter.
  109. *
  110. * @author yansongda <me@yansongda.cn>
  111. *
  112. * @return $this
  113. */
  114. public function setFormatter(FormatterInterface $formatter): self
  115. {
  116. $this->formatter = $formatter;
  117. return $this;
  118. }
  119. /**
  120. * getFormatter.
  121. *
  122. * @author yansongda <me@yansongda.cn>
  123. */
  124. public function getFormatter(): FormatterInterface
  125. {
  126. if (is_null($this->formatter)) {
  127. $this->formatter = $this->createFormatter();
  128. }
  129. return $this->formatter;
  130. }
  131. /**
  132. * createFormatter.
  133. *
  134. * @author yansongda <me@yansongda.cn>
  135. */
  136. public function createFormatter(): LineFormatter
  137. {
  138. return new LineFormatter(
  139. "%datetime% > %channel%.%level_name% > %message% %context% %extra%\n\n",
  140. null,
  141. false,
  142. true
  143. );
  144. }
  145. /**
  146. * setHandler.
  147. *
  148. * @author yansongda <me@yansongda.cn>
  149. *
  150. * @return $this
  151. */
  152. public function setHandler(AbstractHandler $handler): self
  153. {
  154. $this->handler = $handler;
  155. return $this;
  156. }
  157. /**
  158. * getHandler.
  159. *
  160. * @author yansongda <me@yansongda.cn>
  161. *
  162. * @throws \Exception
  163. */
  164. public function getHandler(): AbstractHandler
  165. {
  166. if (is_null($this->handler)) {
  167. $this->handler = $this->createHandler();
  168. }
  169. return $this->handler;
  170. }
  171. /**
  172. * createHandler.
  173. *
  174. * @author yansongda <me@yansongda.cn>
  175. *
  176. * @throws \Exception
  177. *
  178. * @return \Monolog\Handler\RotatingFileHandler|\Monolog\Handler\StreamHandler
  179. */
  180. public function createHandler(): AbstractHandler
  181. {
  182. $file = $this->config['file'] ?? sys_get_temp_dir().'/logs/'.$this->config['identify'].'.log';
  183. if ('single' === $this->config['type']) {
  184. return new StreamHandler($file, $this->config['level']);
  185. }
  186. return new RotatingFileHandler($file, $this->config['max_files'], $this->config['level']);
  187. }
  188. /**
  189. * setConfig.
  190. *
  191. * @author yansongda <me@yansongda.cn>
  192. *
  193. * @return $this
  194. */
  195. public function setConfig(array $config): self
  196. {
  197. $this->config = array_merge($this->config, $config);
  198. return $this;
  199. }
  200. /**
  201. * getConfig.
  202. *
  203. * @author yansongda <me@yansongda.cn>
  204. */
  205. public function getConfig(): array
  206. {
  207. return $this->config;
  208. }
  209. }