Skip to content

Instantly share code, notes, and snippets.

@csinghdev
Created May 7, 2020 15:43
Show Gist options
  • Save csinghdev/6e7b3a6568917481006e8624f06a4ac4 to your computer and use it in GitHub Desktop.
Save csinghdev/6e7b3a6568917481006e8624f06a4ac4 to your computer and use it in GitHub Desktop.
Middleware for API Logging - Laravel
<?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