Update: 2026-05-03 18:19:24
This commit is contained in:
@@ -1,15 +1,14 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* Standardized JSON Responses with Integrated Logging
|
* Standardized JSON Responses with Multi-Level Logging
|
||||||
*/
|
*/
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
function json_response(bool $success, $data = null, ?string $message = null, int $code = 200) {
|
function json_response(bool $success, $data = null, ?string $message = null, int $code = 200) {
|
||||||
// 1. Logging Logic
|
// 1. Prepare Log Entry
|
||||||
$logEntry = sprintf(
|
$logEntry = sprintf(
|
||||||
"[%s] %s %s | Code: %d | Success: %s | Message: %s | Data: %s\n",
|
"API %s %s | Code: %d | Success: %s | Message: %s | Data: %s",
|
||||||
date('Y-m-d H:i:s'),
|
|
||||||
$_SERVER['REQUEST_METHOD'] ?? 'CLI',
|
$_SERVER['REQUEST_METHOD'] ?? 'CLI',
|
||||||
$_SERVER['REQUEST_URI'] ?? '',
|
$_SERVER['REQUEST_URI'] ?? '',
|
||||||
$code,
|
$code,
|
||||||
@@ -18,16 +17,25 @@ function json_response(bool $success, $data = null, ?string $message = null, int
|
|||||||
json_encode($data, JSON_UNESCAPED_UNICODE)
|
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';
|
$logDir = STORAGE_PATH . '/logs';
|
||||||
$logFile = $logDir . '/app.log';
|
$logFile = $logDir . '/app.log';
|
||||||
|
|
||||||
if (!is_dir($logDir)) {
|
try {
|
||||||
mkdir($logDir, 0755, true);
|
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);
|
// 4. HTTP Response
|
||||||
|
|
||||||
// 2. HTTP Response
|
|
||||||
header('Content-Type: application/json; charset=utf-8');
|
header('Content-Type: application/json; charset=utf-8');
|
||||||
http_response_code($code);
|
http_response_code($code);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user