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');
});
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With