Browse Source

关于日志的修改

lizhen_gitee 4 tháng trước cách đây
mục cha
commit
2e1c8a0b37
2 tập tin đã thay đổi với 40 bổ sung7 xóa
  1. 9 0
      app/Utils/AppResult.php
  2. 31 7
      app/Utils/Control/ActionUtil.php

+ 9 - 0
app/Utils/AppResult.php

@@ -4,6 +4,7 @@ declare(strict_types=1);
 
 namespace App\Utils;
 
+use App\Utils\Control\ActionUtil;
 use Hyperf\Context\ApplicationContext;
 use Hyperf\HttpMessage\Stream\SwooleStream;
 use Hyperf\HttpServer\Contract\ResponseInterface;
@@ -250,6 +251,14 @@ class AppResult
      * @throws \Psr\Container\NotFoundExceptionInterface
      */
     private static function response($data){
+        $action = ActionUtil::getInstance()->get();
+        if (!empty($action)){
+            if (!empty($data['result']) && strlen(json_encode($data['result'])) > 1000){
+                $data['result'] = '数据太多,不记录';
+            }
+            LogUtil::info('响应结果', $action['controller'], $action['action'],$data);
+        }
+
         $response = self::container()->get(ResponseInterface::class);
         return $response->withAddedHeader('content-type', 'application/json; charset=utf-8')->withBody(new SwooleStream($data));
     }

+ 31 - 7
app/Utils/Control/ActionUtil.php

@@ -2,31 +2,55 @@
 
 namespace App\Utils\Control;
 
+use Hyperf\Coroutine\Coroutine;
 use Hyperf\HttpServer\Router\Dispatched;
 use Psr\Http\Message\ServerRequestInterface;
 
 class ActionUtil
 {
-    private static string $prefix = 'App\Controller\\';
+private static string $prefix = 'App\Controller\\';
+
+protected array $actions = [];// 控制器信息
+
+    protected static array $instance = [];
+
+    public static function getInstance(): ActionUtil
+    {
+        //协程id
+        $coroutine_id = Coroutine::id();
+        if (empty(self::$instance[$coroutine_id])) {
+            self::$instance[$coroutine_id] = new self();
+
+            Coroutine::defer(function () use ($coroutine_id) {
+                unset(self::$instance[$coroutine_id]);
+            });
+    }
+    return self::$instance[$coroutine_id];
+    }
+
     /**
      * 获取当前路由地址
      * @param ServerRequestInterface $request
      * @param string $project
      * @return array
      */
-    public static function actions(ServerRequestInterface $request, string $project = 'Api'): array
+    public function actions(ServerRequestInterface $request, string $project = 'Api'): array
     {
         $action     = $request->getAttribute(Dispatched::class)->handler->callback ?? [];
         $controller = str_replace(self::$prefix.$project . '\\', '', $action[0] ?? '');
         $controller = str_replace('\\', '/', $controller);
         if (!empty($action[1])) {
-            $res['controller'] = $controller ?? 'CommonController';
-            $res['action']     = $action[1];
+            $this->actions['controller'] = $controller ?? 'CommonController';
+            $this->actions['action']     = $action[1];
         } else {
-            $res['controller'] = 'CommonController';
-            $res['action']     = 'common';
+            $this->actions['controller'] = 'CommonController';
+            $this->actions['action']     = 'common';
         }
-        return $res;
+        return $this->actions;
     }
 
+    public function get()
+    {
+        return $this->actions;
+    }
 }