I was using below code for logging each and every request and response for my API but now it's not working for Laravel 5.2.
I have tried to use https://laravel.com/docs/5.2/middleware#terminable-middleware but not succeed.
use Closure;
use Illuminate\Contracts\Routing\TerminableMiddleware;
use Illuminate\Support\Facades\Log;
class LogAfterRequest implements TerminableMiddleware {
public function handle($request, Closure $next)
{
return $next($request);
}
public function terminate($request, $response)
{
$logFile = 'log.txt';
Log::useDailyFiles(storage_path().'/logs/'.$logFile);
Log::info('app.requests', ['request' => $request->all(), 'response' => $response->getContent()]);
}
}
Can anyone suggest me the solution?
Assuming you use
web
group for your routes.php, you should add inapp/Kernel.php
in$middlewareGroups
forweb
the following middleware:Your
routes.php
should look like this:I have got the solution. the issue was that i have added "die" in controller method due to which terminate function is not executing and so no log generated.