Log.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2021 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace think;
  13. use InvalidArgumentException;
  14. use Psr\Log\LoggerInterface;
  15. use think\event\LogWrite;
  16. use think\helper\Arr;
  17. use think\log\Channel;
  18. use think\log\ChannelSet;
  19. /**
  20. * 日志管理类
  21. * @package think
  22. * @mixin Channel
  23. */
  24. class Log extends Manager implements LoggerInterface
  25. {
  26. const EMERGENCY = 'emergency';
  27. const ALERT = 'alert';
  28. const CRITICAL = 'critical';
  29. const ERROR = 'error';
  30. const WARNING = 'warning';
  31. const NOTICE = 'notice';
  32. const INFO = 'info';
  33. const DEBUG = 'debug';
  34. const SQL = 'sql';
  35. protected $namespace = '\\think\\log\\driver\\';
  36. /**
  37. * 默认驱动
  38. * @return string|null
  39. */
  40. public function getDefaultDriver()
  41. {
  42. return $this->getConfig('default');
  43. }
  44. /**
  45. * 获取日志配置
  46. * @access public
  47. * @param null|string $name 名称
  48. * @param mixed $default 默认值
  49. * @return mixed
  50. */
  51. public function getConfig(string $name = null, $default = null)
  52. {
  53. if (!is_null($name)) {
  54. return $this->app->config->get('log.' . $name, $default);
  55. }
  56. return $this->app->config->get('log');
  57. }
  58. /**
  59. * 获取渠道配置
  60. * @param string $channel
  61. * @param null $name
  62. * @param null $default
  63. * @return array
  64. */
  65. public function getChannelConfig($channel, $name = null, $default = null)
  66. {
  67. if ($config = $this->getConfig("channels.{$channel}")) {
  68. return Arr::get($config, $name, $default);
  69. }
  70. throw new InvalidArgumentException("Channel [$channel] not found.");
  71. }
  72. /**
  73. * driver()的别名
  74. * @param string|array $name 渠道名
  75. * @return Channel|ChannelSet
  76. */
  77. public function channel($name = null)
  78. {
  79. if (is_array($name)) {
  80. return new ChannelSet($this, $name);
  81. }
  82. return $this->driver($name);
  83. }
  84. protected function resolveType(string $name)
  85. {
  86. return $this->getChannelConfig($name, 'type', 'file');
  87. }
  88. public function createDriver(string $name)
  89. {
  90. $driver = parent::createDriver($name);
  91. $lazy = !$this->getChannelConfig($name, "realtime_write", false) && !$this->app->runningInConsole();
  92. $allow = array_merge($this->getConfig("level", []), $this->getChannelConfig($name, "level", []));
  93. return new Channel($name, $driver, $allow, $lazy, $this->app->event);
  94. }
  95. protected function resolveConfig(string $name)
  96. {
  97. return $this->getChannelConfig($name);
  98. }
  99. /**
  100. * 清空日志信息
  101. * @access public
  102. * @param string|array $channel 日志通道名
  103. * @return $this
  104. */
  105. public function clear($channel = '*')
  106. {
  107. if ('*' == $channel) {
  108. $channel = array_keys($this->drivers);
  109. }
  110. $this->channel($channel)->clear();
  111. return $this;
  112. }
  113. /**
  114. * 关闭本次请求日志写入
  115. * @access public
  116. * @param string|array $channel 日志通道名
  117. * @return $this
  118. */
  119. public function close($channel = '*')
  120. {
  121. if ('*' == $channel) {
  122. $channel = array_keys($this->drivers);
  123. }
  124. $this->channel($channel)->close();
  125. return $this;
  126. }
  127. /**
  128. * 获取日志信息
  129. * @access public
  130. * @param string $channel 日志通道名
  131. * @return array
  132. */
  133. public function getLog(string $channel = null): array
  134. {
  135. return $this->channel($channel)->getLog();
  136. }
  137. /**
  138. * 保存日志信息
  139. * @access public
  140. * @return bool
  141. */
  142. public function save(): bool
  143. {
  144. /** @var Channel $channel */
  145. foreach ($this->drivers as $channel) {
  146. $channel->save();
  147. }
  148. return true;
  149. }
  150. /**
  151. * 记录日志信息
  152. * @access public
  153. * @param mixed $msg 日志信息
  154. * @param string $type 日志级别
  155. * @param array $context 替换内容
  156. * @param bool $lazy
  157. * @return $this
  158. */
  159. public function record($msg, string $type = 'info', array $context = [], bool $lazy = true)
  160. {
  161. $channel = $this->getConfig('type_channel.' . $type);
  162. $this->channel($channel)->record($msg, $type, $context, $lazy);
  163. return $this;
  164. }
  165. /**
  166. * 实时写入日志信息
  167. * @access public
  168. * @param mixed $msg 调试信息
  169. * @param string $type 日志级别
  170. * @param array $context 替换内容
  171. * @return $this
  172. */
  173. public function write($msg, string $type = 'info', array $context = [])
  174. {
  175. return $this->record($msg, $type, $context, false);
  176. }
  177. /**
  178. * 注册日志写入事件监听
  179. * @param $listener
  180. * @return Event
  181. */
  182. public function listen($listener)
  183. {
  184. return $this->app->event->listen(LogWrite::class, $listener);
  185. }
  186. /**
  187. * 记录日志信息
  188. * @access public
  189. * @param string $level 日志级别
  190. * @param mixed $message 日志信息
  191. * @param array $context 替换内容
  192. * @return void
  193. */
  194. public function log($level, $message, array $context = []): void
  195. {
  196. $this->record($message, $level, $context);
  197. }
  198. /**
  199. * 记录emergency信息
  200. * @access public
  201. * @param mixed $message 日志信息
  202. * @param array $context 替换内容
  203. * @return void
  204. */
  205. public function emergency($message, array $context = []): void
  206. {
  207. $this->log(__FUNCTION__, $message, $context);
  208. }
  209. /**
  210. * 记录警报信息
  211. * @access public
  212. * @param mixed $message 日志信息
  213. * @param array $context 替换内容
  214. * @return void
  215. */
  216. public function alert($message, array $context = []): void
  217. {
  218. $this->log(__FUNCTION__, $message, $context);
  219. }
  220. /**
  221. * 记录紧急情况
  222. * @access public
  223. * @param mixed $message 日志信息
  224. * @param array $context 替换内容
  225. * @return void
  226. */
  227. public function critical($message, array $context = []): void
  228. {
  229. $this->log(__FUNCTION__, $message, $context);
  230. }
  231. /**
  232. * 记录错误信息
  233. * @access public
  234. * @param mixed $message 日志信息
  235. * @param array $context 替换内容
  236. * @return void
  237. */
  238. public function error($message, array $context = []): void
  239. {
  240. $this->log(__FUNCTION__, $message, $context);
  241. }
  242. /**
  243. * 记录warning信息
  244. * @access public
  245. * @param mixed $message 日志信息
  246. * @param array $context 替换内容
  247. * @return void
  248. */
  249. public function warning($message, array $context = []): void
  250. {
  251. $this->log(__FUNCTION__, $message, $context);
  252. }
  253. /**
  254. * 记录notice信息
  255. * @access public
  256. * @param mixed $message 日志信息
  257. * @param array $context 替换内容
  258. * @return void
  259. */
  260. public function notice($message, array $context = []): void
  261. {
  262. $this->log(__FUNCTION__, $message, $context);
  263. }
  264. /**
  265. * 记录一般信息
  266. * @access public
  267. * @param mixed $message 日志信息
  268. * @param array $context 替换内容
  269. * @return void
  270. */
  271. public function info($message, array $context = []): void
  272. {
  273. $this->log(__FUNCTION__, $message, $context);
  274. }
  275. /**
  276. * 记录调试信息
  277. * @access public
  278. * @param mixed $message 日志信息
  279. * @param array $context 替换内容
  280. * @return void
  281. */
  282. public function debug($message, array $context = []): void
  283. {
  284. $this->log(__FUNCTION__, $message, $context);
  285. }
  286. /**
  287. * 记录sql信息
  288. * @access public
  289. * @param mixed $message 日志信息
  290. * @param array $context 替换内容
  291. * @return void
  292. */
  293. public function sql($message, array $context = []): void
  294. {
  295. $this->log(__FUNCTION__, $message, $context);
  296. }
  297. public function __call($method, $parameters)
  298. {
  299. $this->log($method, ...$parameters);
  300. }
  301. }