How to log each response within laravel 5.2

I used the code below to register each request and response for my API, but now it does not work for Laravel 5.2.

I tried using https://laravel.com/docs/5.2/middleware#terminable-middleware , but failed.

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 someone suggest me a solution?

+3
source share
2 answers

I have a solution. the problem was that I added "die" in the controller method, due to which the completion function is not executed, and therefore the log is not generated.

0
source

, web .php, app/Kernel.php $middlewareGroups web :

\App\Http\Middleware\LogAfterRequest ::class,

routes.php :

Route::group(['middleware' => 'web'], function () {
  // here you put all the routes
});
+1

Source: https://habr.com/ru/post/1687412/


All Articles