apilogs
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (8.3) (push) Has been cancelled
tests / ci (8.4) (push) Has been cancelled
tests / ci (8.5) (push) Has been cancelled

This commit is contained in:
Ovidiu U
2026-04-04 08:41:21 +01:00
parent 9e0aebc729
commit 097f1b0529
11 changed files with 618 additions and 73 deletions

View File

@@ -0,0 +1,45 @@
<?php
namespace App\Services;
use App\Models\ApiLog;
use Illuminate\Http\Client\Response;
use Throwable;
class ApiLogger
{
/**
* Execute an HTTP request and log it to api_logs.
*
* The callable must return an Illuminate\Http\Client\Response.
* Exceptions are logged and re-thrown so the caller handles them.
*
* @param callable(): Response $request
*/
public function send(string $service, string $method, string $url, callable $request): Response
{
$start = microtime(true);
$statusCode = null;
$error = null;
try {
$response = $request();
$statusCode = $response->status();
return $response;
} catch (Throwable $e) {
$error = $e->getMessage();
throw $e;
} finally {
ApiLog::create([
'service' => $service,
'method' => strtoupper($method),
'url' => $url,
'status_code' => $statusCode,
'duration_ms' => (int) round((microtime(true) - $start) * 1000),
'error' => $error,
]);
}
}
}