How to Save Laravel rest Api logs into db

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

How to Save Laravel REST API Logs into Your Database: A Developer's Guide

As developers building robust RESTful APIs with Laravel, understanding how to capture meaningful request and response data is crucial for debugging, auditing, security analysis, and performance monitoring. While Laravel provides an excellent foundation for building APIs, it does not automatically generate detailed, structured logs of every API interaction directly into a database by default.

The short answer to whether Laravel has a built-in setting to enable comprehensive API logging is no, not in the sense of automatically saving full request/response payloads to a dedicated table upon every hit. However, this is not a roadblock. As senior developers, we have the freedom to implement exactly the logging mechanism we need by leveraging Laravel’s powerful features like Middleware, Events, and Eloquent ORM.

This post will walk you through the practical steps and best practices for implementing robust API logging in your Laravel application.

Why Structured Logging is Essential for APIs

When dealing with APIs, logs must capture more than just errors. To effectively monitor an endpoint, you need context:

  1. Request Context: IP address, User Agent, Request Method (GET, POST), and query parameters.
  2. Response Context: Status code (200, 404, 500), response size, and potentially sanitized response bodies.
  3. Entity Context: Which user or resource was involved in the transaction.

Saving this information into a relational database (like MySQL or PostgreSQL) allows you to run complex queries, track usage trends, identify bottlenecks, and perform security audits far more effectively than simple text file logging. This approach aligns perfectly with the principles of building scalable applications, much like the architecture promoted by teams at laravelcompany.com.

Implementing Custom API Logging in Laravel

Since we need custom data storage, we must build a system. The most effective way to achieve this is by intercepting the request/response cycle and persisting the data using Eloquent Models.

Step 1: Design the Database Schema

First, create a migration to define the structure for your logs. A simple api_logs table might look like this:

Schema::create('api_logs', function (Blueprint $table) {
    $table->id();
    $table->string('endpoint');       // e.g., /api/v1/users
    $table->string('method');         // e.g., GET, POST
    $table->integer('status_code');    // HTTP response code
    $table->timestamp('created_at');
    $table->json('request_data');     // Store request parameters as a JSON blob
    $table->json('response_data');    // Store relevant parts of the response
    $table->string('user_id')->nullable(); // Foreign key if authenticated
});

Step 2: Intercepting the Request with Middleware

The best place to intercept the request before it hits your controller is using a custom Middleware. This middleware will capture the necessary details.

In your custom middleware, you can access data from the request object and save it after the controller has executed. For complex logging that needs to happen after the response is generated, using Laravel’s Events system is highly recommended.

Step 3: Capturing Payload Data (The Tricky Part)

Capturing the full request body and response body requires careful handling, especially if you are dealing with large payloads or sensitive data. You would typically inject logic into your controller or use a request listener to capture the raw input and output before they are fully processed by the framework.

For example, within a service layer or an event listener triggered after a successful API call:

// Example concept inside a Listener or Service Class
public function handle(ApiRequested $event)
{
    $logData = [
        'endpoint' => $event->route(),
        'method' => $event->method(),
        'status' => $event->response->status(),
        'request' => $event->request->all(), // Capturing query params, headers, etc.
        'response' => $event->response->json()->toArray(), // Saving the response body
    ];

    ApiLog::create($logData);
}

Conclusion

Saving API logs into a database is not a feature you flip on; it is an architectural decision you implement. By combining Laravel’s strong foundation—using Middleware for interception, Events for decoupled communication, and Eloquent for persistence—you can build a highly customized, secure, and scalable logging system. This approach ensures that your application is not just functional, but also fully auditable, providing the necessary visibility to manage any complex service architecture you deploy.