LogUtil.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace app\utils;
  3. /**
  4. * Author:Panda
  5. * Email:joeyoung0314@qq.com
  6. * Class LogUtil
  7. * @package App\Utils
  8. */
  9. class LogUtil {
  10. /**==============日志系统==============**/
  11. static $logArr = array();
  12. static $logRootPath = '';//日志根目录
  13. static $logExtPath = '';//日志根目录02
  14. static $logExtTypePath = 'api/';//默认通道Api
  15. public static function getInstance($logExtTypePath){
  16. self::$logExtTypePath = $logExtTypePath;
  17. }
  18. /**
  19. * 记录各类日志
  20. * 日志级别:普通
  21. * @param string $controller 控制器
  22. * @param string $action 方法
  23. * @param ...$content //日志参数
  24. */
  25. public static function insert(string $controller,string $action, ...$content){
  26. self::writeLog($content,'Api-Middleware-Log', 'request_log',"{$controller}/{$action}",'local.INFO');
  27. }
  28. /**
  29. * debug 日志
  30. * @param ...$content
  31. */
  32. public static function debug(...$content){
  33. self::writeLog($content,'Api-Middleware-Log', 'request_log',"debug",'local.WARNING');
  34. }
  35. /**
  36. * 日志级别:普通
  37. * @param string $logTitle 日志名称
  38. * @param string $logName 日志目录
  39. * @param string $logFile 日志文件名
  40. * @param array $content 日志参数
  41. */
  42. public static function info($logTitle = '', $logName = 'Common', $logFile = 'common',$content = [] ){
  43. self::writeLog($content,$logName,$logFile,$logTitle,'local.INFO');
  44. }
  45. /**
  46. * 日志级别:警告
  47. * @param string $logTitle 日志名称
  48. * @param string $logName 日志目录
  49. * @param string $logFile 日志文件名
  50. * @param array $content 日志参数
  51. */
  52. public static function warning($logTitle = '', $logName = 'Common', $logFile = 'common',$content = [] ){
  53. self::writeLog($content,$logName,$logFile,$logTitle,'local.WARNING');
  54. }
  55. /**
  56. * 日志级别:错误
  57. * @param string $logTitle 日志名称
  58. * @param string $logName 日志目录
  59. * @param string $logFile 日志文件名
  60. * @param array $content 日志参数
  61. */
  62. public static function error($logTitle = '', $logName = 'Common', $logFile = 'common',$content = [] ){
  63. self::writeLog($content,$logName,$logFile,$logTitle,'local.ERROR');
  64. }
  65. /**
  66. * 日志内容保存
  67. * @param $content
  68. * @param string $logName 日志模块名称
  69. * @param string $logFile 日志内容
  70. * @param string $logTitle 日志内容头部
  71. * @param string $status
  72. *
  73. */
  74. private static function writeLog($content, $logName = 'Common', $logFile = 'common', $logTitle = '',$status = 'INFO') {
  75. $logPath = $logName.'_'.$logFile;
  76. $logWhite = self::whiteLog();
  77. if(in_array($logPath,$logWhite)) return;//验证日志黑名单
  78. //获取日志文件目录
  79. if (empty(self::$logRootPath)) self::$logRootPath = realpath(ROOT_PATH) . '/runtime/Log/'.self::$logExtTypePath;
  80. if (empty(self::$logExtPath)) self::$logExtPath = realpath(ROOT_PATH) . '/runtime/Log02/'.self::$logExtTypePath;
  81. if (empty(self::$logArr[$logName][$logFile])) {
  82. self::$logArr[$logName][$logFile] = '================ Start ================'.PHP_EOL;
  83. }
  84. if (is_array($content)) $content = json_encode($content, JSON_UNESCAPED_UNICODE);
  85. if (!empty($logTitle)) $logTitle .= ':';
  86. self::$logArr[$logName][$logFile] .= '['.(date('Y-m-d H:i:s')."] {$status}: {$logTitle}{$content}".PHP_EOL.PHP_EOL);
  87. }
  88. public static function close() {
  89. try{
  90. if (empty(self::$logArr)) return;
  91. //没有Log目录 创建Log02目录
  92. if(!file_exists(self::$logRootPath)){
  93. $tem_path = self::$logExtPath;
  94. if(!file_exists($tem_path)){
  95. mkdir($tem_path, 0777 ,true);
  96. }
  97. $logPath = $tem_path.date('Y_m_d');
  98. }else{
  99. $logPath = self::$logRootPath.date('Y_m_d');
  100. }
  101. //创建日志文件目录
  102. if (!file_exists($logPath)) mkdir($logPath);
  103. //逐个日志文件写入
  104. foreach (self::$logArr as $key => $value) {
  105. $logDir = $logPath.'/'.$key;
  106. if (!file_exists($logDir)) mkdir($logDir , 0777 , true);
  107. self::logEnd($value, $logDir);
  108. }
  109. self::$logArr = null;
  110. }catch (\Exception $e){
  111. return;
  112. }
  113. }
  114. public static function logEnd($data, $logDir) {
  115. foreach ($data as $k => $v) {
  116. //获取日志文件名 避免单个日志文件太大
  117. $count = 1;
  118. while(true) {
  119. //生成日志文件名
  120. $logFile = "{$logDir}".'/'."{$k}_{$count}.log";
  121. //第一次写入日志
  122. if (!is_file($logFile)) break;
  123. //日志文件未大于1M
  124. $file = filesize($logFile) / 1024;
  125. if ($file < 1024 * 100) break;
  126. $count++;
  127. }
  128. $v = rtrim($v);
  129. $v .= PHP_EOL.'================ End ================'.PHP_EOL.PHP_EOL;
  130. error_log($v, 3, $logFile);
  131. // $ch = fopen($logFile, 'ab');
  132. // fwrite($ch, $v);
  133. // fclose($ch);
  134. }
  135. }
  136. /**
  137. * 日志黑名单
  138. * @return array
  139. */
  140. public static function whiteLog()
  141. {
  142. return array(
  143. // 'Framework/PassportController_login',
  144. );
  145. }
  146. }