Created
May 7, 2020 15:43
-
-
Save csinghdev/6e7b3a6568917481006e8624f06a4ac4 to your computer and use it in GitHub Desktop.
Middleware for API Logging - Laravel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Middleware; | |
use Closure; | |
use Illuminate\Support\Facades\Log; | |
class LogRoute | |
{ | |
/** | |
* Handle an incoming request. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param \Closure $next | |
* @return mixed | |
*/ | |
public function handle($request, Closure $next) | |
{ | |
$response = $next($request); | |
if (app()->environment('local')) { | |
$log = [ | |
'URI' => $request->getUri(), | |
'METHOD' => $request->getMethod(), | |
'REQUEST_BODY' => $request->all(), | |
'RESPONSE' => $response->getContent() | |
]; | |
Log::info(json_encode($log)); | |
} | |
return $response; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment