Channel.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: yunwuxin <448901948@qq.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace think\log;
  13. use Psr\Log\LoggerInterface;
  14. use think\contract\LogHandlerInterface;
  15. use think\Event;
  16. use think\event\LogWrite;
  17. class Channel implements LoggerInterface
  18. {
  19. protected $name;
  20. protected $logger;
  21. protected $event;
  22. protected $lazy = true;
  23. /**
  24. * 日志信息
  25. * @var array
  26. */
  27. protected $log = [];
  28. /**
  29. * 关闭日志
  30. * @var array
  31. */
  32. protected $close = false;
  33. /**
  34. * 允许写入类型
  35. * @var array
  36. */
  37. protected $allow = [];
  38. public function __construct(string $name, LogHandlerInterface $logger, array $allow, bool $lazy = true, Event $event = null)
  39. {
  40. $this->name = $name;
  41. $this->logger = $logger;
  42. $this->allow = $allow;
  43. $this->lazy = $lazy;
  44. $this->event = $event;
  45. }
  46. /**
  47. * 关闭通道
  48. */
  49. public function close()
  50. {
  51. $this->clear();
  52. $this->close = true;
  53. }
  54. /**
  55. * 清空日志
  56. */
  57. public function clear()
  58. {
  59. $this->log = [];
  60. }
  61. /**
  62. * 记录日志信息
  63. * @access public
  64. * @param mixed $msg 日志信息
  65. * @param string $type 日志级别
  66. * @param array $context 替换内容
  67. * @param bool $lazy
  68. * @return $this
  69. */
  70. public function record($msg, string $type = 'info', array $context = [], bool $lazy = true)
  71. {
  72. if ($this->close || (!empty($this->allow) && !in_array($type, $this->allow))) {
  73. return $this;
  74. }
  75. if (is_string($msg) && !empty($context)) {
  76. $replace = [];
  77. foreach ($context as $key => $val) {
  78. $replace['{' . $key . '}'] = $val;
  79. }
  80. $msg = strtr($msg, $replace);
  81. }
  82. if (!empty($msg) || 0 === $msg) {
  83. $this->log[$type][] = $msg;
  84. }
  85. if (!$this->lazy || !$lazy) {
  86. $this->save();
  87. }
  88. return $this;
  89. }
  90. /**
  91. * 实时写入日志信息
  92. * @access public
  93. * @param mixed $msg 调试信息
  94. * @param string $type 日志级别
  95. * @param array $context 替换内容
  96. * @return $this
  97. */
  98. public function write($msg, string $type = 'info', array $context = [])
  99. {
  100. return $this->record($msg, $type, $context, false);
  101. }
  102. /**
  103. * 获取日志信息
  104. * @return array
  105. */
  106. public function getLog(): array
  107. {
  108. return $this->log;
  109. }
  110. /**
  111. * 保存日志
  112. * @return bool
  113. */
  114. public function save(): bool
  115. {
  116. $log = $this->log;
  117. if ($this->event) {
  118. $event = new LogWrite($this->name, $log);
  119. $this->event->trigger($event);
  120. $log = $event->log;
  121. }
  122. if ($this->logger->save($log)) {
  123. $this->clear();
  124. return true;
  125. }
  126. return false;
  127. }
  128. /**
  129. * System is unusable.
  130. *
  131. * @param string $message
  132. * @param array $context
  133. *
  134. * @return void
  135. */
  136. public function emergency($message, array $context = [])
  137. {
  138. $this->log(__FUNCTION__, $message, $context);
  139. }
  140. /**
  141. * Action must be taken immediately.
  142. *
  143. * Example: Entire website down, database unavailable, etc. This should
  144. * trigger the SMS alerts and wake you up.
  145. *
  146. * @param string $message
  147. * @param array $context
  148. *
  149. * @return void
  150. */
  151. public function alert($message, array $context = [])
  152. {
  153. $this->log(__FUNCTION__, $message, $context);
  154. }
  155. /**
  156. * Critical conditions.
  157. *
  158. * Example: Application component unavailable, unexpected exception.
  159. *
  160. * @param string $message
  161. * @param array $context
  162. *
  163. * @return void
  164. */
  165. public function critical($message, array $context = [])
  166. {
  167. $this->log(__FUNCTION__, $message, $context);
  168. }
  169. /**
  170. * Runtime errors that do not require immediate action but should typically
  171. * be logged and monitored.
  172. *
  173. * @param string $message
  174. * @param array $context
  175. *
  176. * @return void
  177. */
  178. public function error($message, array $context = [])
  179. {
  180. $this->log(__FUNCTION__, $message, $context);
  181. }
  182. /**
  183. * Exceptional occurrences that are not errors.
  184. *
  185. * Example: Use of deprecated APIs, poor use of an API, undesirable things
  186. * that are not necessarily wrong.
  187. *
  188. * @param string $message
  189. * @param array $context
  190. *
  191. * @return void
  192. */
  193. public function warning($message, array $context = [])
  194. {
  195. $this->log(__FUNCTION__, $message, $context);
  196. }
  197. /**
  198. * Normal but significant events.
  199. *
  200. * @param string $message
  201. * @param array $context
  202. *
  203. * @return void
  204. */
  205. public function notice($message, array $context = [])
  206. {
  207. $this->log(__FUNCTION__, $message, $context);
  208. }
  209. /**
  210. * Interesting events.
  211. *
  212. * Example: User logs in, SQL logs.
  213. *
  214. * @param string $message
  215. * @param array $context
  216. *
  217. * @return void
  218. */
  219. public function info($message, array $context = [])
  220. {
  221. $this->log(__FUNCTION__, $message, $context);
  222. }
  223. /**
  224. * Detailed debug information.
  225. *
  226. * @param string $message
  227. * @param array $context
  228. *
  229. * @return void
  230. */
  231. public function debug($message, array $context = [])
  232. {
  233. $this->log(__FUNCTION__, $message, $context);
  234. }
  235. /**
  236. * Logs with an arbitrary level.
  237. *
  238. * @param mixed $level
  239. * @param string $message
  240. * @param array $context
  241. *
  242. * @return void
  243. */
  244. public function log($level, $message, array $context = [])
  245. {
  246. $this->record($message, $level, $context);
  247. }
  248. public function __call($method, $parameters)
  249. {
  250. $this->log($method, ...$parameters);
  251. }
  252. }