Skip to content

Instantly share code, notes, and snippets.

@Shelob9
Created October 3, 2016 18:42
Show Gist options
  • Save Shelob9/174f6093c6b388f325157ba3dea82a46 to your computer and use it in GitHub Desktop.
Save Shelob9/174f6093c6b388f325157ba3dea82a46 to your computer and use it in GitHub Desktop.
Log all request to Laravel app. Based on http://blog.phakeapps.com/2015/06/23/log-every-request-and-response-in-laravel-5/ updated for Laravel 5.3
namespace App\Http\Middleware;
use Illuminate\Support\Facades\Log;
class LogAfterRequest {
public function handle($request, \Closure $next)
{
return $next($request);
}
public function terminate($request, $response)
{
Log::info('app.requests', ['request' => $request->all(), 'response' => $response]);
}
}
@cp-vrkansagara
Copy link

terminate will not work if you are working with DB::beginTransaction(); and DB::commit();, and if you still wanted to work with it you need to do a rollback or commit before sending a response to the client.

I would suggest using bellow code at the handle method this will work all time.

app('db')
            ->table('client_history')
            ->insert([
                'request' => $request->getContent(),
                'response' => $response->content(),
                'created_at' => Carbon::now(),
            ]);

hope this works for others.

@GabrieleCicconetti
Copy link

Thank you this is incredibly usefull ❤️

@cp-vrkansagara
Copy link

cp-vrkansagara commented Sep 15, 2020 via email

@salmagomaa
Copy link

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment