From 43ffb1f726593b242ea25747b4ab0425b140d908 Mon Sep 17 00:00:00 2001 From: Steve <34465153+xxl4@users.noreply.github.com> Date: Thu, 18 Jul 2024 09:58:04 +0800 Subject: [PATCH] #4 use config for the input and output log defult false --- src/Config/Apis.php | 3 ++- src/Http/Middleware/AssignRequestId.php | 16 +++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/Config/Apis.php b/src/Config/Apis.php index 44c839b..28fd2f5 100644 --- a/src/Config/Apis.php +++ b/src/Config/Apis.php @@ -3,5 +3,6 @@ 'name' => 'Apis', 'version' => '1.0.7', 'versionNum' => '107', - 'enable_input_log' => env("APIS_ENABLE_INPUT_LOG", true), // Enable input log for all requests + 'enable_input_log' => env("APIS_ENABLE_INPUT_LOG", false), // Enable input log for all requests + 'enable_output_log' => env("APIS_ENABLE_OUTPUT_LOG", false), // Enable output log for all requests ]; \ No newline at end of file diff --git a/src/Http/Middleware/AssignRequestId.php b/src/Http/Middleware/AssignRequestId.php index 2700449..7197e12 100644 --- a/src/Http/Middleware/AssignRequestId.php +++ b/src/Http/Middleware/AssignRequestId.php @@ -21,9 +21,9 @@ public function handle(Request $request, Closure $next): Response Log::withContext([ 'request-id' => $requestId ]); - - if(config("apis.enable_input_log")) { - Log::info('Request', [ + + if(config("Apis.enable_input_log")) { + Log::info('Request Input', [ 'request_id' => $requestId, 'method' => $request->getMethod(), 'url' => $request->getUri(), @@ -32,9 +32,19 @@ public function handle(Request $request, Closure $next): Response } $response = $next($request); + + $response->headers->set('Request-Id', $requestId); + if(config("Apis.enable_output_log")) { + Log::info('Response', [ + 'request_id' => $requestId, + 'status' => $response->status(), + 'output' => $response->getContent() + ]); + + } return $response; }