Home > Net >  How to log Laravel get and post requests
How to log Laravel get and post requests

Time:01-22

How can I log all post and get requests going in and out of Laravel Luma ?

Our routes look like this :

    $router->group(['prefix' => 'mini'], function () use ($router) {
    $router->get('/', 'Controller@index');
    $router->post('/', 'Controller@index');
    $router->get('/paginate', 'Controller@paginate');
    $router->post('/paginate', 'Controller@paginate');
    $router->get('/debug', 'Controller@debug');
    $router->post('/debug', 'Controller@debug');
    $router->get('/debug_sql', 'Controller@debug_sql');
    $router->post('/debug_sql', 'Controller@debug_sql');
});

CodePudding user response:

You should create a middleware:

<?php

namespace App\Http\Middleware;

use Closure;

class LogRequest
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $response = $next($request);
        app('log')->info("Request Captured", $request->all());

        return $response;
    }
}

Then reference it in your bootstrap/app.php:

$app->middleware([
     App\Http\Middleware\LogRequest::class
]);

This will produce the following in your lumen.log:

[2022-01-20 22:16:35] local.INFO: Request Captured {"hello":"there"} 

for a given GET request /?hello=there

  •  Tags:  
  • Related