LogUtil.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. * @param string $logTitle 日志名称
  21. * @param string $logName 日志目录
  22. * @param string $logFile 日志文件名
  23. * @param array $content 日志参数
  24. */
  25. public static function info($logTitle = '', $logName = 'Common', $logFile = 'common',$content = [] ){
  26. self::writeLog($content,$logName,$logFile,$logTitle,'local.INFO');
  27. }
  28. /**
  29. * 日志级别:警告
  30. * @param string $logTitle 日志名称
  31. * @param string $logName 日志目录
  32. * @param string $logFile 日志文件名
  33. * @param array $content 日志参数
  34. */
  35. public static function warning($logTitle = '', $logName = 'Common', $logFile = 'common',$content = [] ){
  36. self::writeLog($content,$logName,$logFile,$logTitle,'local.WARNING');
  37. }
  38. /**
  39. * 日志级别:错误
  40. * @param string $logTitle 日志名称
  41. * @param string $logName 日志目录
  42. * @param string $logFile 日志文件名
  43. * @param array $content 日志参数
  44. */
  45. public static function error($logTitle = '', $logName = 'Common', $logFile = 'common',$content = [] ){
  46. self::writeLog($content,$logName,$logFile,$logTitle,'local.ERROR');
  47. }
  48. /**
  49. * 日志内容保存
  50. * @param $content
  51. * @param string $logName 日志模块名称
  52. * @param string $logFile 日志内容
  53. * @param string $logTitle 日志内容头部
  54. * @param string $status
  55. *
  56. */
  57. private static function writeLog($content, $logName = 'Common', $logFile = 'common', $logTitle = '',$status = 'INFO') {
  58. $logPath = $logName.'_'.$logFile;
  59. $logWhite = self::whiteLog();
  60. if(in_array($logPath,$logWhite)) return;//验证日志黑名单
  61. //if(is_array($content) && isset($content['route']) && $content['route'] == 'Param_uploadFileImg') return;//黑名单
  62. //获取日志文件目录
  63. if (empty(self::$logRootPath)) self::$logRootPath = realpath(base_path()) . '/storage/logs/Log/'.self::$logExtTypePath;
  64. if (empty(self::$logExtPath)) self::$logExtPath = realpath(base_path()) . '/storage/logs/Log02/'.self::$logExtTypePath;
  65. if (empty(self::$logArr[$logName][$logFile])) {
  66. self::$logArr[$logName][$logFile] = '================ Start ================'.PHP_EOL;
  67. }
  68. if (is_array($content)) $content = json_encode($content, JSON_UNESCAPED_UNICODE);
  69. if (!empty($logTitle)) $logTitle .= ':';
  70. self::$logArr[$logName][$logFile] .= '['.(date('Y-m-d H:i:s')."] {$status}: {$logTitle}{$content}".PHP_EOL.PHP_EOL);
  71. }
  72. public static function close() {
  73. try{
  74. if (empty(self::$logArr)) return;
  75. //没有Log目录 创建Log02目录
  76. if(!file_exists(self::$logRootPath)){
  77. $tem_path = self::$logExtPath;
  78. if(!file_exists($tem_path)){
  79. mkdir($tem_path, 0777 ,true);
  80. }
  81. $logPath = $tem_path.date('Y_m_d');
  82. }else{
  83. $logPath = self::$logRootPath.date('Y_m_d');
  84. }
  85. //创建日志文件目录
  86. if (!file_exists($logPath)) mkdir($logPath);
  87. //逐个日志文件写入
  88. foreach (self::$logArr as $key => $value) {
  89. $logDir = $logPath.'/'.$key;
  90. if (!file_exists($logDir)) mkdir($logDir , 0777 , true);
  91. self::logEnd($value, $logDir);
  92. }
  93. self::$logArr = null;
  94. }catch (Exception $e){
  95. return;
  96. }
  97. }
  98. public static function logEnd($data, $logDir) {
  99. foreach ($data as $k => $v) {
  100. //获取日志文件名 避免单个日志文件太大
  101. $count = 1;
  102. while(true) {
  103. //生成日志文件名
  104. $logFile = "{$logDir}".'/'."{$k}_{$count}.log";
  105. //第一次写入日志
  106. if (!is_file($logFile)) break;
  107. //日志文件未大于1M
  108. $file = filesize($logFile) / 1024 / 1024;// 单位M
  109. if ($file < 2) break;
  110. $count++;
  111. }
  112. $v = rtrim($v);
  113. $v .= PHP_EOL.'================ End ================'.PHP_EOL.PHP_EOL;
  114. error_log($v, 3, $logFile);
  115. // $ch = fopen($logFile, 'ab');
  116. // fwrite($ch, $v);
  117. // fclose($ch);
  118. }
  119. }
  120. /**
  121. * 日志黑名单
  122. * @return array
  123. */
  124. public static function whiteLog()
  125. {
  126. return array(
  127. // 'Framework/PassportController_login',
  128. );
  129. }
  130. }