Initial profile site commit
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
// ============================================================
|
||||
// error_config.php — API 파일 최상단에 require 추가
|
||||
// 위치: /web/my_profile/api/error_config.php
|
||||
//
|
||||
// 사용법: 각 api/*.php 파일 최상단에 아래 한 줄 추가
|
||||
// require_once __DIR__ . '/error_config.php';
|
||||
// ============================================================
|
||||
|
||||
// 로그 파일 경로 (uploads 바깥, 웹 접근 불가 경로 권장)
|
||||
define('LOG_FILE', dirname(__DIR__) . '/logs/app.log');
|
||||
|
||||
// logs/ 디렉토리 자동 생성
|
||||
$log_dir = dirname(LOG_FILE);
|
||||
if (!is_dir($log_dir)) {
|
||||
mkdir($log_dir, 0750, true);
|
||||
}
|
||||
|
||||
// PHP 에러를 파일에만 기록 (화면 노출 X)
|
||||
ini_set('display_errors', '0');
|
||||
ini_set('log_errors', '1');
|
||||
ini_set('error_log', LOG_FILE);
|
||||
error_reporting(E_ALL);
|
||||
|
||||
// ── 커스텀 로거 ───────────────────────────────────────────
|
||||
/**
|
||||
* 앱 로그 기록
|
||||
*
|
||||
* @param string $level 'INFO' | 'WARN' | 'ERROR'
|
||||
* @param string $msg 메시지
|
||||
* @param array $ctx 추가 컨텍스트 (선택)
|
||||
*/
|
||||
function app_log(string $level, string $msg, array $ctx = []): void {
|
||||
$ts = date('Y-m-d H:i:s');
|
||||
$ip = $_SERVER['REMOTE_ADDR'] ?? '-';
|
||||
$method = $_SERVER['REQUEST_METHOD'] ?? '-';
|
||||
$uri = $_SERVER['REQUEST_URI'] ?? '-';
|
||||
$ctx_str = $ctx ? ' | ' . json_encode($ctx, JSON_UNESCAPED_UNICODE) : '';
|
||||
$line = "[$ts][$level] $ip $method $uri | $msg$ctx_str" . PHP_EOL;
|
||||
file_put_contents(LOG_FILE, $line, FILE_APPEND | LOCK_EX);
|
||||
}
|
||||
|
||||
// ── 로그 로테이션 (10MB 초과 시 백업) ─────────────────────
|
||||
if (file_exists(LOG_FILE) && filesize(LOG_FILE) > 10 * 1024 * 1024) {
|
||||
rename(LOG_FILE, LOG_FILE . '.' . date('Ymd'));
|
||||
}
|
||||
|
||||
// ── 미처리 예외 핸들러 ────────────────────────────────────
|
||||
set_exception_handler(function (Throwable $e) {
|
||||
app_log('ERROR', $e->getMessage(), [
|
||||
'file' => $e->getFile(),
|
||||
'line' => $e->getLine(),
|
||||
]);
|
||||
http_response_code(500);
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode(['success' => false, 'error' => '서버 오류가 발생했습니다.']);
|
||||
exit;
|
||||
});
|
||||
Reference in New Issue
Block a user