enabled = API_LOGGING_ENABLED; $db = new Database(); $this->pdo = $db->getConnection(); } public static function getInstance() { if (self::$instance === null) { self::$instance = new self(); } return self::$instance; } public function logRequest($method, $path, $params = [], $body = null) { if (!$this->enabled) { return; } try { $stmt = $this->pdo->prepare(" INSERT INTO api_logs (type, method, path, params, body) VALUES ('REQUEST', :method, :path, :params, :body) "); $methodValue = is_array($method) ? (json_encode($method) ?: '[array]') : (string)$method; $pathValue = is_array($path) ? (json_encode($path) ?: '[array]') : (string)$path; $paramsValue = is_array($params) ? (json_encode($params) ?: '[array]') : (string)$params; $bodyValue = null; if ($body) { $bodyValue = is_array($body) ? (json_encode($body) ?: '[array]') : (string)$body; } $stmt->execute([ ':method' => $methodValue, ':path' => $pathValue, ':params' => $paramsValue, ':body' => $bodyValue ]); } catch (Exception $e) { error_log('Failed to log request: ' . $e->getMessage()); } } public function logResponse($method, $path, $statusCode, $response) { if (!$this->enabled) { return; } try { $stmt = $this->pdo->prepare(" INSERT INTO api_logs (type, method, path, status_code, response) VALUES ('RESPONSE', :method, :path, :status_code, :response) "); $stmt->execute([ ':method' => is_array($method) ? (json_encode($method) ?: '[array]') : (string)$method, ':path' => is_array($path) ? (json_encode($path) ?: '[array]') : (string)$path, ':status_code' => $statusCode, ':response' => (json_encode($response) ?: '[encoding_failed]') ]); } catch (Exception $e) { error_log('Failed to log response: ' . $e->getMessage()); } } public function logError($method, $path, $error) { if (!$this->enabled) { return; } try { $stmt = $this->pdo->prepare(" INSERT INTO api_logs (type, method, path, error) VALUES ('ERROR', :method, :path, :error) "); $stmt->execute([ ':method' => is_array($method) ? (json_encode($method) ?: '[array]') : (string)$method, ':path' => is_array($path) ? (json_encode($path) ?: '[array]') : (string)$path, ':error' => is_array($error) ? (json_encode($error) ?: '[array]') : (string)$error ]); } catch (Exception $e) { error_log('Failed to log error: ' . $e->getMessage()); } } public function logDebug($message) { if (!$this->enabled) { return; } try { $stmt = $this->pdo->prepare(" INSERT INTO api_logs (type, message) VALUES ('DEBUG', :message) "); $stmt->execute([ ':message' => is_array($message) ? (json_encode($message) ?: '[array]') : (string)$message ]); } catch (Exception $e) { error_log('Failed to log debug: ' . $e->getMessage()); } } }