로그 생성 수정
This commit is contained in:
@@ -11,26 +11,39 @@
|
|||||||
* @param string $level 로그 레벨 (INFO, ERROR, DEBUG 등)
|
* @param string $level 로그 레벨 (INFO, ERROR, DEBUG 등)
|
||||||
*/
|
*/
|
||||||
function writeLog($message, $level = 'ERROR') {
|
function writeLog($message, $level = 'ERROR') {
|
||||||
// 1. 로그 저장 경로 설정 (프로젝트 루트의 logs 폴더)
|
try {
|
||||||
$logBaseDir = __DIR__ . '/logs';
|
// 1. 로그 저장 경로 설정 (프로젝트 루트의 logs 폴더)
|
||||||
$Dir = $logBaseDir . '/';
|
$logBaseDir = __DIR__ . '/logs';
|
||||||
|
$Dir = $logBaseDir . '/';
|
||||||
|
|
||||||
// 2. 폴더가 없으면 생성 (연/월 구조로 관리하면 파일이 너무 많아지는 것을 방지)
|
// 2. 폴더가 없으면 생성
|
||||||
if (!is_dir($Dir)) {
|
if (!is_dir($Dir)) {
|
||||||
@mkdir($Dir, 0777, true);
|
if (!mkdir($Dir, 0777, true) && !is_dir($Dir)) {
|
||||||
|
error_log("Failed to create log directory: $Dir");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
chmod($Dir, 0777); // 권한 명시적 설정
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. 파일명 결정
|
||||||
|
$logFile = $Dir . date('Y-m-d') . '.log';
|
||||||
|
|
||||||
|
// 4. 로그 포맷팅 (시간 [레벨] 메시지)
|
||||||
|
$timestamp = date('Y-m-d H:i:s');
|
||||||
|
$singleLineMessage = str_replace(["\r", "\n", "\t"], " ", $message);
|
||||||
|
$formattedMessage = "[$timestamp] [$level] $singleLineMessage" . PHP_EOL;
|
||||||
|
|
||||||
|
// 5. 파일 기록
|
||||||
|
$result = file_put_contents($logFile, $formattedMessage, FILE_APPEND | LOCK_EX);
|
||||||
|
|
||||||
|
if ($result === false) {
|
||||||
|
error_log("Failed to write log file: $logFile");
|
||||||
|
} else {
|
||||||
|
chmod($logFile, 0666); // 로그 파일 권한 설정
|
||||||
|
}
|
||||||
|
} catch (Exception $e) {
|
||||||
|
error_log("writeLog Exception: " . $e->getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. 파일명 결정 (예: logs/2025/12/2025-12-22.log)
|
|
||||||
$logFile = $Dir . date('Y-m-d') . '.log';
|
|
||||||
// 4. 로그 포맷팅 (시간 [레벨] 메시지)
|
|
||||||
$timestamp = date('Y-m-d H:i:s');
|
|
||||||
|
|
||||||
$singleLineMessage = str_replace(["\r", "\n", "\t"], " ", $message);
|
|
||||||
|
|
||||||
$formattedMessage = "[$timestamp] [$level] $singleLineMessage" . PHP_EOL;
|
|
||||||
|
|
||||||
// 5. 파일 기록 (FILE_APPEND로 기존 내용 뒤에 추가)
|
|
||||||
file_put_contents($logFile, $formattedMessage, FILE_APPEND);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 도우미 함수 정의
|
// 도우미 함수 정의
|
||||||
|
|||||||
Reference in New Issue
Block a user