How to log Laravel get and post requests
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
How to Log Laravel GET and POST Requests: A Developer's Guide
As developers building robust applications with Laravel, understanding how to track incoming HTTP requests is fundamental for debugging, auditing, and monitoring application behavior. Whether you are working with a standard Laravel setup or a streamlined environment like Lumen, logging the details of GET and POST requests is crucial.
This guide will walk you through the most effective methods for logging these requests in your Laravel application, focusing on practical implementation rather than just theory.
The Best Approach: Implementing Custom Middleware
The most scalable and maintainable way to log every request entering your application is by utilizing Laravel's powerful Middleware system. Middleware allows you to inspect or modify the HTTP request before it hits the controller logic, making it the perfect place for logging request details like the method, URI, and IP address.
For a dedicated logging mechanism, we can create a custom middleware that executes on every request.
Step 1: Creating the Request Logger Middleware
First, generate the middleware using the Artisan command:
php artisan make:middleware RequestLoggerNext, open the newly created file (app/Http/Middleware/RequestLogger.php) and implement the logic to capture the necessary data within the handle method. We will use the Log facade to write the information to your configured log files.
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
class RequestLogger
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response) $next
* @return \Illuminate\Http\Response
*/
public function handle(Request $request, Closure $next)
{
// Log the incoming request details
Log::info('Incoming Request: ' . $request->method() . ' ' . $request->fullUrl());
// Pass the request to the next layer
$response = $next($request);
// Optional: Log the outgoing response details (useful for tracking latency or errors)
Log::info('Outgoing Response: Status Code ' . $response->getStatusCode() . ' for ' . $request->fullUrl());
return $response;
}
}Step 2: Registering the Middleware
After creating the class, you must register it in your application's middleware stack, typically within app/Http/Kernel.php. You can apply this middleware globally to ensure all routes—including those defined under your /mini prefix—are logged.
In app/Http/Kernel.php, add your new middleware to the $middleware array (for global application) or a route-specific group if you only want it on certain prefixes:
protected $middleware = [
// ... other middleware
\App\Http\Middleware\RequestLogger::class, // Add it here
];Step 3: Testing the Logs
Now, when you hit your routes—for example, a GET /mini or a POST /mini—the information will be recorded in your log files (usually storage/logs/laravel.log). This provides a clear audit trail of exactly which request method and URL was processed. This approach aligns perfectly with the principles of building structured applications, much like those promoted by Laravel Company.
Alternative: Logging within Controllers for Specific Actions
While middleware is best for global tracking, you can also log specific actions directly within your controller methods. This is useful when you need to log context-specific data related only to that particular operation (e.g., logging the data received in a POST request).
For instance, inside your Controller@index method:
public function index(Request $request)
{
// Log specific details for this action
Log::info('Received GET request on /mini');
if ($request->isMethod('POST')) {
Log::info('Received POST request on /mini with data: ' . $request->all());
// Process the form data here
}
// ... rest of your logic
}Conclusion
For comprehensive logging of all GET and POST requests entering your Laravel application—including tracking the method and URI across routes like those defined in your example—custom middleware is the superior architectural choice. It separates cross-cutting concerns (like logging) from business logic, keeping your controllers clean and focused on handling data. By leveraging this pattern, you ensure that every interaction with your API or web interface is properly recorded, which is a cornerstone of secure and maintainable software development, echoing the quality standards found in frameworks like Laravel Company.