Logging requests
Add the HasApiLogs trait to the model that makes the requests (typically your User
model):
use CodeTech\ApiLogs\Traits\HasApiLogs;
class User extends Authenticatable
{
use HasApiLogs;
}
To start logging requests made to your API, append the middleware to the api
middleware group in your bootstrap/app.php:
use CodeTech\ApiLogs\Http\Middleware\LogApiRequest;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Middleware;
return Application::configure(basePath: dirname(__DIR__))
// ...
->withMiddleware(function (Middleware $middleware) {
$middleware->appendToGroup('api', LogApiRequest::class);
})
// ...
->create();
Only authenticated requests are logged. Each log stores the URL, HTTP method, client
IP, request data, request headers, response data and the request duration — with
sensitive values redacted — and is linked to the authenticated user
through a polymorphic causer relation:
$user->apiLogs; // all ApiLog entries for the user
$apiLog->causer; // the model that made the request