From dfe15f7e8877ee4021c2d06be0defe8429791f20 Mon Sep 17 00:00:00 2001 From: wolfcode <37436228+wolf-leo@users.noreply.github.com> Date: Sat, 12 Oct 2024 17:21:56 +0800 Subject: [PATCH] =?UTF-8?q?feat(admin):=20=E6=96=B0=E5=A2=9E=E8=BF=87?= =?UTF-8?q?=E6=BB=A4=E4=B8=8D=E9=9C=80=E8=A6=81=E8=AE=B0=E5=BD=95=E5=90=8E?= =?UTF-8?q?=E5=8F=B0=E6=97=A5=E5=BF=97=E7=9A=84=E6=96=B9=E6=B3=95=20add=20?= =?UTF-8?q?ignoreLog=20property=20to=20exclude=20log=20recording=20for=20s?= =?UTF-8?q?pecific=20actions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add 'ignoreLog' property to the Log controller to specify actions that should not be logged - Implement logic in SystemLog middleware to check if the current action is in the ignoreLog list - Skip logging for actions listed in ignoreLog, improving performance and reducing log clutter Signed-off-by: wolfcode <37436228+wolf-leo@users.noreply.github.com> --- app/admin/controller/system/Log.php | 1 + app/admin/middleware/SystemLog.php | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/app/admin/controller/system/Log.php b/app/admin/controller/system/Log.php index 3564495..323c227 100644 --- a/app/admin/controller/system/Log.php +++ b/app/admin/controller/system/Log.php @@ -22,6 +22,7 @@ use think\response\Json; */ class Log extends AdminController { + protected array $ignoreLog = ['record']; public function __construct(App $app) { diff --git a/app/admin/middleware/SystemLog.php b/app/admin/middleware/SystemLog.php index 2779bd5..838c761 100644 --- a/app/admin/middleware/SystemLog.php +++ b/app/admin/middleware/SystemLog.php @@ -7,9 +7,11 @@ use app\admin\service\annotation\NodeAnnotation; use app\admin\service\SystemLogService; use app\common\traits\JumpTrait; use app\Request; +use ReflectionClass; use Closure; use Doctrine\Common\Annotations\AnnotationReader; use Doctrine\Common\Annotations\DocParser; +use ReflectionException; class SystemLog { @@ -26,9 +28,13 @@ class SystemLog 'mobile', ]; + /** + * @throws ReflectionException + */ public function handle(Request $request, Closure $next) { - $params = $request->param(); + $response = $next($request); + $params = $request->param(); if (isset($params['s'])) unset($params['s']); foreach ($params as $key => $val) { in_array($key, $this->sensitiveParams) && $params[$key] = "***********"; @@ -39,9 +45,18 @@ class SystemLog if (env('APP_DEBUG')) { trace(['url' => $url, 'method' => $method, 'params' => $params,], 'requestDebugInfo'); } - $response = $next($request); if ($request->isAjax()) { if (in_array($method, ['post', 'put', 'delete'])) { + + $controller = $request->controller(); + if (str_contains($controller, '.')) $controller = str_replace('.', '\\', $controller); + $action = $request->action(); + $controllerClass = 'app\\admin\\controller\\' . $controller; + $classObj = new ReflectionClass($controllerClass); + $properties = $classObj->getDefaultProperties(); + $ignoreLog = $properties['ignoreLog'] ?? []; + if (in_array($action, $ignoreLog)) return $response; + $title = ''; try { $pathInfo = $request->pathinfo();