Browse Source

接口日志

lizhen_gitee 9 months ago
parent
commit
9ef142e2dc

+ 1 - 0
application/api/library/ExceptionHandle.php

@@ -4,6 +4,7 @@ namespace app\api\library;
 
 use Exception;
 use think\exception\Handle;
+use app\utils\LogUtil;
 
 /**
  * 自定义API模块的错误显示

+ 3 - 5
application/common/controller/Api.php

@@ -13,6 +13,8 @@ use think\Request;
 use think\Response;
 use think\Route;
 use think\Validate;
+use Redis;
+use app\utils\LogUtil;
 
 /**
  * API控制器基类
@@ -67,7 +69,7 @@ class Api
     /**
      * @var int 日志类型 1 文件;2sql
      */
-    public $logType = 1;
+    public $logType = 2;
 
     /**
      * 构造方法
@@ -447,10 +449,6 @@ class Api
     }
 
     protected function request_log_update($log_result){
-        $actionname     = $this->request->action();
-        if(strtolower($actionname) == 'givegifttoyou'){
-            //return true;
-        }
 
         if ($this->logType === 1){
             if (strlen(json_encode($log_result['data'])) > 1000) {

+ 16 - 1
application/config.php

@@ -19,6 +19,7 @@ return [
     'app_namespace'          => 'app',
     // 应用调试模式
     'app_debug'              => Env::get('app.debug', false),
+    'app_debug'              => true,
     // 应用Trace
     'app_trace'              => Env::get('app.trace', false),
     // 应用模式状态
@@ -300,7 +301,7 @@ return [
         //插件纯净模式,插件启用后是否删除插件目录的application、public和assets文件夹
         'addon_pure_mode'       => true,
         //允许跨域的域名,多个以,分隔
-        'cors_request_domain'   => 'localhost,127.0.0.1',
+        'cors_request_domain'   => 'localhost,127.0.0.1,*',
         //版本号
         'version'               => '1.5.0.20240328',
         //API接口地址
@@ -318,4 +319,18 @@ return [
         'secret_key' => "cbmCvofYk8fRQVhsOqIh2R4qlwIoQ8td",
     ],
 
+    //redis配置
+    'redis'                  => [
+        'host'     => '127.0.0.1',
+        'port'      => 6379,
+        'redis_pwd' => '',
+        'redis_selectdb' => 0,
+    ],
+
+    //后台网站配置
+    'newconfiggroup' =>
+        [
+            'website' => '全站基础配置',
+        ],
+
 ];

+ 169 - 0
application/utils/LogUtil.php

@@ -0,0 +1,169 @@
+<?php
+namespace app\utils;
+
+
+/**
+ * Author:Panda
+ * Email:joeyoung0314@qq.com
+ * Class LogUtil
+ * @package App\Utils
+ */
+class LogUtil {
+    /**==============日志系统==============**/
+
+    static $logArr = array();
+    static $logRootPath = '';//日志根目录
+    static $logExtPath = '';//日志根目录02
+    static $logExtTypePath = 'api/';//默认通道Api
+
+    public static function getInstance($logExtTypePath){
+        self::$logExtTypePath = $logExtTypePath;
+    }
+
+    /**
+     * 记录各类日志
+     * 日志级别:普通
+     * @param string $controller 控制器
+     * @param string $action 方法
+     * @param ...$content //日志参数
+     */
+    public static function insert(string $controller,string $action, ...$content){
+        self::writeLog($content,'Api-Middleware-Log', 'request_log',"{$controller}/{$action}",'local.INFO');
+    }
+
+    /**
+     * debug 日志
+     * @param ...$content
+     */
+    public static function debug(...$content){
+        self::writeLog($content,'Api-Middleware-Log', 'request_log',"debug",'local.WARNING');
+    }
+
+    /**
+     * 日志级别:普通
+     * @param string $logTitle 日志名称
+     * @param string $logName 日志目录
+     * @param string $logFile 日志文件名
+     * @param array $content 日志参数
+     */
+    public static function info($logTitle = '', $logName = 'Common', $logFile = 'common',$content = [] ){
+        self::writeLog($content,$logName,$logFile,$logTitle,'local.INFO');
+    }
+
+    /**
+     * 日志级别:警告
+     * @param string $logTitle 日志名称
+     * @param string $logName 日志目录
+     * @param string $logFile 日志文件名
+     * @param array $content 日志参数
+     */
+    public static function warning($logTitle = '', $logName = 'Common', $logFile = 'common',$content = [] ){
+        self::writeLog($content,$logName,$logFile,$logTitle,'local.WARNING');
+    }
+
+    /**
+     * 日志级别:错误
+     * @param string $logTitle 日志名称
+     * @param string $logName 日志目录
+     * @param string $logFile 日志文件名
+     * @param array $content 日志参数
+     */
+    public static function error($logTitle = '', $logName = 'Common', $logFile = 'common',$content = [] ){
+        self::writeLog($content,$logName,$logFile,$logTitle,'local.ERROR');
+    }
+
+    /**
+     * 日志内容保存
+     * @param $content
+     * @param string $logName 日志模块名称
+     * @param string $logFile 日志内容
+     * @param string $logTitle 日志内容头部
+     * @param string $status
+     *
+     */
+    private static function writeLog($content, $logName = 'Common', $logFile = 'common', $logTitle = '',$status = 'INFO') {
+        $logPath = $logName.'_'.$logFile;
+        $logWhite = self::whiteLog();
+        if(in_array($logPath,$logWhite)) return;//验证日志黑名单
+        //获取日志文件目录
+        if (empty(self::$logRootPath)) self::$logRootPath = realpath(ROOT_PATH) . '/runtime/Log/'.self::$logExtTypePath;
+
+        if (empty(self::$logExtPath))  self::$logExtPath  =  realpath(ROOT_PATH) . '/runtime/Log02/'.self::$logExtTypePath;
+
+        if (empty(self::$logArr[$logName][$logFile])) {
+            self::$logArr[$logName][$logFile] = '================ Start ================'.PHP_EOL;
+        }
+
+        if (is_array($content)) $content = json_encode($content, JSON_UNESCAPED_UNICODE);
+        if (!empty($logTitle)) $logTitle .= ':';
+        self::$logArr[$logName][$logFile] .= '['.(date('Y-m-d H:i:s')."] {$status}: {$logTitle}{$content}".PHP_EOL.PHP_EOL);
+    }
+
+    public static function close() {
+        try{
+
+            if (empty(self::$logArr)) return;
+
+            //没有Log目录 创建Log02目录
+            if(!file_exists(self::$logRootPath)){
+                $tem_path = self::$logExtPath;
+                if(!file_exists($tem_path)){
+                    mkdir($tem_path, 0777 ,true);
+                }
+                $logPath = $tem_path.date('Y_m_d');
+            }else{
+                $logPath = self::$logRootPath.date('Y_m_d');
+            }
+
+            //创建日志文件目录
+            if (!file_exists($logPath)) mkdir($logPath);
+
+            //逐个日志文件写入
+            foreach (self::$logArr as $key => $value) {
+                $logDir = $logPath.'/'.$key;
+                if (!file_exists($logDir)) mkdir($logDir , 0777 , true);
+                self::logEnd($value, $logDir);
+            }
+            self::$logArr = null;
+        }catch (\Exception $e){
+            return;
+        }
+    }
+    public static function logEnd($data, $logDir) {
+        foreach ($data as $k => $v) {
+            //获取日志文件名 避免单个日志文件太大
+            $count = 1;
+            while(true) {
+                //生成日志文件名
+                $logFile = "{$logDir}".'/'."{$k}_{$count}.log";
+
+                //第一次写入日志
+                if (!is_file($logFile)) break;
+
+                //日志文件未大于1M
+                $file = filesize($logFile) / 1024;
+                if ($file < 1024 * 100) break;
+
+                $count++;
+            }
+            $v = rtrim($v);
+            $v .= PHP_EOL.'================ End ================'.PHP_EOL.PHP_EOL;
+
+            error_log($v, 3, $logFile);
+//            $ch = fopen($logFile, 'ab');
+//            fwrite($ch, $v);
+//            fclose($ch);
+        }
+    }
+
+    /**
+     * 日志黑名单
+     * @return array
+     */
+    public static function whiteLog()
+    {
+        return array(
+//            'Framework/PassportController_login',
+        );
+    }
+}