LogManager.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace think;
  13. use InvalidArgumentException;
  14. use Psr\Log\LoggerInterface;
  15. use think\Container;
  16. /**
  17. * 日志管理类
  18. */
  19. class LogManager implements LoggerInterface
  20. {
  21. const EMERGENCY = 'emergency';
  22. const ALERT = 'alert';
  23. const CRITICAL = 'critical';
  24. const ERROR = 'error';
  25. const WARNING = 'warning';
  26. const NOTICE = 'notice';
  27. const INFO = 'info';
  28. const DEBUG = 'debug';
  29. const SQL = 'sql';
  30. /**
  31. * 日志信息
  32. * @var array
  33. */
  34. protected $log = [];
  35. /**
  36. * 日志通道
  37. * @var array
  38. */
  39. protected $channel = [];
  40. /**
  41. * 配置参数
  42. * @var array
  43. */
  44. protected $config = [];
  45. /**
  46. * 日志写入驱动
  47. * @var array
  48. */
  49. protected $driver = [];
  50. /**
  51. * 日志处理
  52. *
  53. * @var array
  54. */
  55. protected $processor = [];
  56. /**
  57. * 关闭日志(渠道)
  58. * @var array
  59. */
  60. protected $close = [];
  61. /**
  62. * (通道)允许写入类型
  63. * @var array
  64. */
  65. protected $allow = [];
  66. /**
  67. * 是否控制台执行
  68. * @var bool
  69. */
  70. protected $isCli = false;
  71. /**
  72. * 初始化
  73. * @access public
  74. */
  75. public function init(array $config = [])
  76. {
  77. $this->config = $config;
  78. if (isset($this->config['processor'])) {
  79. $this->processor($this->config['processor']);
  80. }
  81. if (!empty($this->config['close'])) {
  82. $this->close['*'] = true;
  83. }
  84. if (!empty($this->config['level'])) {
  85. $this->allow['*'] = $this->config['level'];
  86. }
  87. $this->isCli = $this->runningInConsole();
  88. $this->channel();
  89. }
  90. /**
  91. * 是否运行在命令行下
  92. * @return bool
  93. */
  94. public function runningInConsole()
  95. {
  96. return php_sapi_name() === 'cli' || php_sapi_name() === 'phpdbg';
  97. }
  98. /**
  99. * 获取日志配置
  100. * @access public
  101. * @return array
  102. */
  103. public function getConfig(): array
  104. {
  105. return $this->config;
  106. }
  107. /**
  108. * 注册一个日志回调处理
  109. *
  110. * @param callable $callback 回调
  111. * @param string $channel 日志通道名
  112. * @return void
  113. */
  114. public function processor(callable $callback, string $channel = '*'): void
  115. {
  116. $this->processor[$channel][] = $callback;
  117. }
  118. /**
  119. * 切换日志通道
  120. * @access public
  121. * @param string|array $name 日志通道名
  122. * @return $this
  123. */
  124. public function channel($name = '')
  125. {
  126. if ('' == $name) {
  127. $name = $this->config['default'] ?? 'think';
  128. }
  129. $names = (array) $name;
  130. foreach ($names as $name) {
  131. if (!isset($this->config['channels'][$name])) {
  132. throw new InvalidArgumentException('Undefined log config:' . $name);
  133. }
  134. $config = $this->config['channels'][$name];
  135. if (!empty($config['processor'])) {
  136. $this->processor($config['processor'], $name);
  137. }
  138. if (!empty($config['close'])) {
  139. $this->close[$name] = true;
  140. }
  141. if (!empty($config['level'])) {
  142. $this->allow[$name] = $config['level'];
  143. }
  144. }
  145. $this->channel = $names;
  146. return $this;
  147. }
  148. /**
  149. * 实例化日志写入驱动
  150. * @access public
  151. * @param string $name 日志通道名
  152. * @return object
  153. */
  154. protected function driver(string $name)
  155. {
  156. if (!isset($this->driver[$name])) {
  157. $config = $this->config['channels'][$name];
  158. $type = !empty($config['type']) ? $config['type'] : 'File';
  159. $config['is_cli'] = $this->isCli;
  160. $this->driver[$name] = Container::factory($type, '\\think\\log\\driver\\', $config);
  161. }
  162. return $this->driver[$name];
  163. }
  164. /**
  165. * 获取日志信息
  166. * @access public
  167. * @param string $channel 日志通道
  168. * @return array
  169. */
  170. public function getLog(string $channel = ''): array
  171. {
  172. $channel = $channel ?: array_shift($this->channel);
  173. return $this->log[$channel] ?? [];
  174. }
  175. /**
  176. * 记录日志信息
  177. * @access public
  178. * @param mixed $msg 日志信息
  179. * @param string $type 日志级别
  180. * @param array $context 替换内容
  181. * @return $this
  182. */
  183. public function record($msg, string $type = 'info', array $context = [])
  184. {
  185. if (!empty($this->allow['*']) && !in_array($type, $this->allow['*'])) {
  186. return $this;
  187. }
  188. if (is_string($msg) && !empty($context)) {
  189. $replace = [];
  190. foreach ($context as $key => $val) {
  191. $replace['{' . $key . '}'] = $val;
  192. }
  193. $msg = strtr($msg, $replace);
  194. }
  195. if (isset($this->config['type_channel'][$type])) {
  196. $channels = (array) $this->config['type_channel'][$type];
  197. } else {
  198. $channels = $this->channel;
  199. }
  200. foreach ($channels as $channel) {
  201. if (empty($this->allow[$channel]) || in_array($type, $this->allow[$channel])) {
  202. $this->channelLog($channel, $msg, $type);
  203. }
  204. }
  205. return $this;
  206. }
  207. /**
  208. * 记录通道日志
  209. * @access public
  210. * @param string $channel 日志通道
  211. * @param mixed $msg 日志信息
  212. * @param string $type 日志级别
  213. * @return void
  214. */
  215. protected function channelLog(string $channel, $msg, string $type): void
  216. {
  217. if (!empty($this->close['*']) || !empty($this->close[$channel])) {
  218. return;
  219. }
  220. if ($this->isCli || !empty($this->config['channels'][$channel]['realtime_write'])) {
  221. // 实时写入
  222. $this->write($msg, $type, true, $channel);
  223. } else {
  224. $this->log[$channel][$type][] = $msg;
  225. }
  226. }
  227. /**
  228. * 清空日志信息
  229. * @access public
  230. * @param string $channel 日志通道名
  231. * @return $this
  232. */
  233. public function clear(string $channel = '')
  234. {
  235. if ($channel) {
  236. $this->log[$channel] = [];
  237. } else {
  238. $this->log = [];
  239. }
  240. return $this;
  241. }
  242. /**
  243. * 关闭本次请求日志写入
  244. * @access public
  245. * @param string $channel 日志通道名
  246. * @return $this
  247. */
  248. public function close(string $channel = '*')
  249. {
  250. $this->close[$channel] = true;
  251. $this->clear('*' == $channel ? '' : $channel);
  252. return $this;
  253. }
  254. /**
  255. * 保存日志信息
  256. * @access public
  257. * @return bool
  258. */
  259. public function save(): bool
  260. {
  261. if (!empty($this->close['*'])) {
  262. return true;
  263. }
  264. foreach ($this->log as $channel => $logs) {
  265. if (!empty($this->close[$channel])) {
  266. continue;
  267. }
  268. $result = $this->saveChannel($channel, $logs);
  269. if ($result) {
  270. $this->log[$channel] = [];
  271. }
  272. }
  273. return true;
  274. }
  275. /**
  276. * 保存某个通道的日志信息
  277. * @access protected
  278. * @param string $channel 日志通道名
  279. * @param array $log 日志信息
  280. * @return bool
  281. */
  282. protected function saveChannel(string $channel, array $log = []): bool
  283. {
  284. // 日志处理
  285. $processors = array_merge($this->processor[$channel] ?? [], $this->processor['*'] ?? []);
  286. foreach ($processors as $callback) {
  287. $log = $callback($log, $channel);
  288. if (false === $log) {
  289. return false;
  290. }
  291. }
  292. return $this->driver($channel)->save($log);
  293. }
  294. /**
  295. * 实时写入日志信息
  296. * @access public
  297. * @param mixed $msg 调试信息
  298. * @param string $type 日志级别
  299. * @param bool $force 是否强制写入
  300. * @param string $channel 日志通道
  301. * @return bool
  302. */
  303. public function write($msg, string $type = 'info', bool $force = false, $channel = ''): bool
  304. {
  305. if (empty($this->allow['*'])) {
  306. $force = true;
  307. }
  308. $log = [];
  309. if (true === $force || in_array($type, $this->allow['*'])) {
  310. $log[$type][] = $msg;
  311. } else {
  312. return false;
  313. }
  314. // 写入日志
  315. $channels = $channel ? (array) $channel : $this->channel;
  316. foreach ($channels as $channel) {
  317. if (empty($this->allow[$channel]) || in_array($type, $this->allow[$channel])) {
  318. $this->saveChannel($channel, $log);
  319. }
  320. }
  321. return true;
  322. }
  323. /**
  324. * 记录日志信息
  325. * @access public
  326. * @param string $level 日志级别
  327. * @param mixed $message 日志信息
  328. * @param array $context 替换内容
  329. * @return void
  330. */
  331. public function log($level, $message, array $context = []): void
  332. {
  333. $this->record($message, $level, $context);
  334. }
  335. /**
  336. * 记录emergency信息
  337. * @access public
  338. * @param mixed $message 日志信息
  339. * @param array $context 替换内容
  340. * @return void
  341. */
  342. public function emergency($message, array $context = []): void
  343. {
  344. $this->log(__FUNCTION__, $message, $context);
  345. }
  346. /**
  347. * 记录警报信息
  348. * @access public
  349. * @param mixed $message 日志信息
  350. * @param array $context 替换内容
  351. * @return void
  352. */
  353. public function alert($message, array $context = []): void
  354. {
  355. $this->log(__FUNCTION__, $message, $context);
  356. }
  357. /**
  358. * 记录紧急情况
  359. * @access public
  360. * @param mixed $message 日志信息
  361. * @param array $context 替换内容
  362. * @return void
  363. */
  364. public function critical($message, array $context = []): void
  365. {
  366. $this->log(__FUNCTION__, $message, $context);
  367. }
  368. /**
  369. * 记录错误信息
  370. * @access public
  371. * @param mixed $message 日志信息
  372. * @param array $context 替换内容
  373. * @return void
  374. */
  375. public function error($message, array $context = []): void
  376. {
  377. $this->log(__FUNCTION__, $message, $context);
  378. }
  379. /**
  380. * 记录warning信息
  381. * @access public
  382. * @param mixed $message 日志信息
  383. * @param array $context 替换内容
  384. * @return void
  385. */
  386. public function warning($message, array $context = []): void
  387. {
  388. $this->log(__FUNCTION__, $message, $context);
  389. }
  390. /**
  391. * 记录notice信息
  392. * @access public
  393. * @param mixed $message 日志信息
  394. * @param array $context 替换内容
  395. * @return void
  396. */
  397. public function notice($message, array $context = []): void
  398. {
  399. $this->log(__FUNCTION__, $message, $context);
  400. }
  401. /**
  402. * 记录一般信息
  403. * @access public
  404. * @param mixed $message 日志信息
  405. * @param array $context 替换内容
  406. * @return void
  407. */
  408. public function info($message, array $context = []): void
  409. {
  410. $this->log(__FUNCTION__, $message, $context);
  411. }
  412. /**
  413. * 记录调试信息
  414. * @access public
  415. * @param mixed $message 日志信息
  416. * @param array $context 替换内容
  417. * @return void
  418. */
  419. public function debug($message, array $context = []): void
  420. {
  421. $this->log(__FUNCTION__, $message, $context);
  422. }
  423. /**
  424. * 记录sql信息
  425. * @access public
  426. * @param mixed $message 日志信息
  427. * @param array $context 替换内容
  428. * @return void
  429. */
  430. public function sql($message, array $context = []): void
  431. {
  432. $this->log(__FUNCTION__, $message, $context);
  433. }
  434. public function __call($method, $args)
  435. {
  436. array_unshift($args, $method);
  437. call_user_func_array([$this, 'log'], $args);
  438. }
  439. }