Files
confirms/app/Helpers/log_helper.php
2026-03-10 19:38:13 +09:00

45 lines
1.7 KiB
PHP

<?php
if (! function_exists('write_custom_log')) {
/**
* 전용 로그 기록 함수 (Worker, API 리시버 등 어디서나 사용 가능)
*/
function write_custom_log($message, $level = 'INFO', $type = 'service')
{
$logDir = WRITEPATH . 'logs';
if (!is_dir($logDir)) {
@mkdir($logDir, 0777, true);
}
// --- 호출 위치 추적 로직 추가 ---
// debug_backtrace는 호출 스택을 가져옵니다.
// [0]은 현재 함수(write_custom_log), [1]은 이 함수를 호출한 곳입니다.
$bt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
$caller = $bt[1] ?? null;
$fileInfo = $bt[0] ?? null; // 파일명과 라인수는 호출 시점인 0번 인덱스에 들어있음
$location = 'unknown';
if ($caller) {
$class = $caller['class'] ?? '';
$func = $caller['function'] ?? '';
$line = $fileInfo['line'] ?? '0';
// 클래스명에서 Namespace 제외하고 클래스명만 짧게 가져오기 (선택 사항)
$classShort = substr(strrchr($class, "\\"), 1) ?: $class;
$location = "{$classShort}::{$func}:{$line}";
}
// ----------------------------
$suffix = ($type === 'failed') ? '-failed' : '';
$logFile = $logDir . '/log-' . date('Y-m-d') . $suffix . '.log';
$timestamp = date('Y-m-d H:i:s');
$singleLine = str_replace(["\r", "\n", "\t"], " ", $message);
// 포맷에 [$location] 추가
$formatted = "[$timestamp] [$level] [$location] $singleLine" . PHP_EOL;
@file_put_contents($logFile, $formatted, FILE_APPEND);
}
}