Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log Laravel get and post requests

Tags:

php

laravel

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');
});
like image 306
Daniel Baker Avatar asked Sep 15 '25 13:09

Daniel Baker


1 Answers

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

like image 98
Jazerix Avatar answered Sep 17 '25 05:09

Jazerix