Log.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. /**
  3. * Created by hnh.
  4. * Email: 1123416584@qq.com
  5. * Blog: blog.hnh117.com
  6. */
  7. namespace app\admin\library;
  8. class Log
  9. {
  10. protected $dir = null;
  11. protected $config = null;
  12. protected $error = null;
  13. // 过虑条件
  14. protected $filter = [];
  15. // 分隔符
  16. protected $separator = '---------------------------------------------------------------';
  17. public function __construct()
  18. {
  19. $config = config('log');
  20. $dir = $config['path'];
  21. $this->config = $config;
  22. $this->dir = rtrim($dir, DIRECTORY_SEPARATOR);
  23. }
  24. /**
  25. * 删除日志根据ID
  26. * @param $file_path 日志文件路径
  27. * @param $ids 要删除的ID
  28. * @return bool 成功返回true,失败返回false
  29. */
  30. public function deleteForId($file_path, $ids)
  31. {
  32. $ids = is_array($ids) ? $ids : compact('ids');
  33. $logArry = $this->getLogs($file_path, []);
  34. foreach ($ids as $id) {
  35. $index = $id - 1;
  36. if (isset($logArry[$index])) {
  37. unset($logArry[$index]);
  38. }
  39. }
  40. $logStr = '';
  41. foreach ($logArry as $item) {
  42. $logStr .= $this->separator.$item['content'];
  43. }
  44. $path = $this->complementLogPath($file_path);
  45. file_put_contents($path, $logStr);
  46. return true;
  47. }
  48. /**
  49. * 获取日志信息
  50. */
  51. public function getInfo($file_path)
  52. {
  53. $path = $this->complementLogPath($file_path);
  54. if (false === $path) {
  55. return false;
  56. }
  57. $info['size'] = format_bytes(filesize($path));
  58. $info['update_time'] = date('Y-m-d H:i:s', filemtime($path));
  59. return $info;
  60. }
  61. /**
  62. * 根据文件相对路径补全路径,自动做安全判断
  63. *
  64. * @param $file
  65. * @return bool|string
  66. */
  67. public function complementLogPath($file)
  68. {
  69. $path = $this->dir . $file;
  70. if (strpos($file, '.') === 0 || false !== strpos($file, '..') || is_dir($path)) {
  71. return false;
  72. }
  73. return $path;
  74. }
  75. /**
  76. * 获取日志
  77. */
  78. public function getLogs($file_path, $filter)
  79. {
  80. $this->filter = $filter;
  81. $path = $this->complementLogPath($file_path);
  82. if (false === $path) {
  83. return false;
  84. }
  85. $strlogs = file_get_contents($path);
  86. $arr = explode($this->separator, $strlogs);
  87. unset($arr[0]);
  88. $row = [];
  89. $levels = [
  90. 'ERROR' => '[ error ]',
  91. 'NOTICE' => '[ notice ]',
  92. 'INFO' => '[ info ]',
  93. 'DEBUG' => '[ debug ]',
  94. 'SQL' => '[ sql ]',
  95. 'LOG' => '[ log ]',
  96. ];
  97. foreach ($arr as $k => $v) {
  98. $regex = '/\[\s(\d+-\d+-\d+T\d+:\d+:\d+).{6}\s\]\s(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s([A-Z]{3,8})\s(.+)/';
  99. preg_match($regex, $v, $matches);
  100. $item['id'] = $k;
  101. $item['time'] = $matches[1];
  102. $item['ip'] = $matches[2];
  103. $item['method'] = $matches[3];
  104. $item['url'] = $matches[4];
  105. $item['content'] = trim($v);
  106. $content = str_replace($matches[0], '', $v);
  107. foreach ($levels as $k => $v) {
  108. if (false !== strpos($content, $v)) {
  109. $item['level'] = $k;
  110. break;
  111. }
  112. }
  113. $this->filter($row, $item);
  114. }
  115. $order = isset($_GET['order']) ? $_GET['order'] : 'asc';
  116. if ($order == 'desc') {
  117. $row = array_reverse($row);
  118. }
  119. return $row;
  120. }
  121. /**
  122. * 过虑筛选选
  123. *
  124. * @param $row
  125. * @param $item
  126. * @return bool
  127. */
  128. public function filter(&$row, $item)
  129. {
  130. $filter = $this->filter;
  131. if (empty($filter)) {
  132. $row[] = $item;
  133. return true;
  134. }
  135. if (isset($filter['level']) && $filter['level'] != strtolower($item['level'])) {
  136. return false;
  137. }
  138. if (isset($filter['method']) && $filter['method'] != strtolower($item['method'])) {
  139. return false;
  140. }
  141. if (isset($filter['url']) && !strpos($item['url'], $filter['url'])) {
  142. return false;
  143. }
  144. if (isset($filter['time'])) {
  145. $itemTimeInt = strtotime($item['time']);
  146. $timeArr = explode(' - ', $filter['time']);
  147. array_walk($timeArr, function (&$v) {
  148. $v = strtotime($v);
  149. });
  150. if ($timeArr[0] > $itemTimeInt || $timeArr[1] < $itemTimeInt) {
  151. return false;
  152. }
  153. }
  154. $row[] = $item;
  155. return true;
  156. }
  157. /**
  158. * 获取日志目录
  159. *
  160. * @return array|bool
  161. */
  162. public function getDirectory()
  163. {
  164. $directory = $this->getSelectedDirectory();
  165. return $directory;
  166. }
  167. /**
  168. * 获取选中最新日志的文件目录
  169. *
  170. * @return mixed
  171. */
  172. public function getSelectedDirectory()
  173. {
  174. $directory = $this->directory($this->dir);
  175. $directory = array_filter($directory, function ($item) {
  176. return !empty($item['children']);
  177. });
  178. $directory = array_merge($directory);
  179. return $this->setSelected($directory);
  180. }
  181. /**
  182. * (简单)自动选中最新日志
  183. *
  184. * @param $directory $array 日志目录数组
  185. * @return mixed
  186. */
  187. public function setSelected($directory)
  188. {
  189. $last = count($directory) - 1;
  190. if (empty($directory[$last])) {
  191. return false;
  192. }
  193. if ($directory[$last]['type'] == 'folder') {
  194. if ($directory[$last]['children'] != []) {
  195. $directory[$last]['children'] = $this->setSelected($directory[$last]['children']);
  196. }
  197. } else {
  198. $directory[$last]['state'] = ['selected' => true];
  199. }
  200. return $directory;
  201. }
  202. /**
  203. * 递归获取目录
  204. *
  205. * @param $dir
  206. * @return array|bool
  207. */
  208. public function directory($dir)
  209. {
  210. $dir = rtrim($dir, DIRECTORY_SEPARATOR);
  211. if (false === is_dir($dir)) {
  212. return false;
  213. }
  214. $fiels = scandir($dir);
  215. $directory = [];
  216. foreach ($fiels as $k => $v) {
  217. if ($v == '.' || $v == '..') {
  218. continue;
  219. }
  220. $path = $dir . DIRECTORY_SEPARATOR . $v;
  221. $id = str_replace($this->dir, '', $path);
  222. if (is_dir($path)) {
  223. $directory[] = [
  224. 'id' => $id,
  225. 'text' => $v,
  226. 'type' => 'folder',
  227. 'children' => $this->directory($path),
  228. ];
  229. } else {
  230. $directory[] = [
  231. 'id' => $id,
  232. 'text' => $v,
  233. 'type' => 'file',
  234. ];
  235. }
  236. }
  237. return $directory;
  238. }
  239. // 是否可以使用
  240. public function isAllow()
  241. {
  242. return $this->config['type'] == 'File' ? true : $this->setError('目前仅支持 File 日志驱动。');
  243. }
  244. public function setError($err)
  245. {
  246. $this->error = $err;
  247. return false;
  248. }
  249. public function getError()
  250. {
  251. return $this->error;
  252. }
  253. }