Skip to content

Instantly share code, notes, and snippets.

@musamamasood
Created December 4, 2019 10:48
Show Gist options
  • Save musamamasood/9826773f6ac22d0ef8c76b1ce4117a99 to your computer and use it in GitHub Desktop.
Save musamamasood/9826773f6ac22d0ef8c76b1ce4117a99 to your computer and use it in GitHub Desktop.
Laravel Middleware for Request & Response
<?php
namespace App\Http\Middleware;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;
class LogAfterRequest {
protected $timestamp;
/**
* @param $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, \Closure $next)
{
return $next($request);
}
/**
* @param $request
* @param $response
*/
public function terminate($request, $response)
{
$this->timestamp = Carbon::now();
$this->log($request, $response);
}
/**
* @param $request
*/
protected function log($request, $response)
{
Log::info('ApiLog Start===========================');
Log::info('Timestamp: ' . $this->timestamp);
Log::info('URL: ' . $request->fullUrl());
Log::info('Method: ' . $request->getMethod());
Log::info('IP Address: ' . $request->getClientIp());
Log::info('Request: ' . json_encode($request->all()) );
Log::info('Response: ' . json_encode($response->original) );
Log::info('Status Code: ' . $response->getStatusCode());
Log::info('ApiLog End===========================');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment