Update: 2026-05-03 18:19:24

This commit is contained in:
Hamza-Ayed
2026-05-03 18:19:24 +03:00
parent 59d766c6d7
commit 2c8ed7e742

View File

@@ -1,15 +1,14 @@
<?php
/**
* Standardized JSON Responses with Integrated Logging
* Standardized JSON Responses with Multi-Level Logging
*/
declare(strict_types=1);
function json_response(bool $success, $data = null, ?string $message = null, int $code = 200) {
// 1. Logging Logic
// 1. Prepare Log Entry
$logEntry = sprintf(
"[%s] %s %s | Code: %d | Success: %s | Message: %s | Data: %s\n",
date('Y-m-d H:i:s'),
"API %s %s | Code: %d | Success: %s | Message: %s | Data: %s",
$_SERVER['REQUEST_METHOD'] ?? 'CLI',
$_SERVER['REQUEST_URI'] ?? '',
$code,
@@ -18,16 +17,25 @@ function json_response(bool $success, $data = null, ?string $message = null, int
json_encode($data, JSON_UNESCAPED_UNICODE)
);
// 2. Log to Standard PHP Error Log (Visible in CloudPanel Logs)
error_log($logEntry);
// 3. Try to log to custom app.log in storage folder
$logDir = STORAGE_PATH . '/logs';
$logFile = $logDir . '/app.log';
if (!is_dir($logDir)) {
mkdir($logDir, 0755, true);
try {
if (!is_dir($logDir)) {
@mkdir($logDir, 0775, true);
}
if (is_writable($logDir) || is_writable($logFile)) {
@file_put_contents($logFile, "[" . date('Y-m-d H:i:s') . "] " . $logEntry . "\n", FILE_APPEND);
}
} catch (\Exception $e) {
// Fallback if filesystem is locked
}
@file_put_contents($logFile, $logEntry, FILE_APPEND);
// 2. HTTP Response
// 4. HTTP Response
header('Content-Type: application/json; charset=utf-8');
http_response_code($code);