45 lines
1.5 KiB
JavaScript
45 lines
1.5 KiB
JavaScript
// util/debugCapture.js
|
|
// 디버그 모드에서 상태/오브젝트를 파일로 저장하고 Claude가 읽을 수 있게 함
|
|
// 사용법:
|
|
// import { debugCapture } from '@/util/debugCapture'
|
|
// debugCapture.state('rackInfos', rackInfos) // 오브젝트 스냅샷 저장
|
|
// debugCapture.error('fetchFailed', error, context) // 에러 저장
|
|
// debugCapture.log('step label', someData) // 로그 저장
|
|
|
|
const isLoggingEnabled = process.env.NEXT_PUBLIC_ENABLE_LOGGING === 'true'
|
|
|
|
async function send(type, label, data) {
|
|
if (!isLoggingEnabled || typeof window === 'undefined') return
|
|
try {
|
|
await fetch('/api/debug', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ type, label, data, timestamp: new Date().toISOString() }),
|
|
})
|
|
} catch {}
|
|
}
|
|
|
|
export const debugCapture = {
|
|
// 오브젝트/상태 스냅샷 - debug/debug-snapshot.json 에 저장
|
|
state: (label, obj) => send('snapshot', label, obj),
|
|
|
|
// 에러 캡처 - debug/debug-errors.json 에 저장
|
|
error: (label, error, context = null) =>
|
|
send('error', label, {
|
|
message: error?.message ?? String(error),
|
|
stack: error?.stack ?? null,
|
|
context,
|
|
}),
|
|
|
|
// 일반 로그 - debug/debug.log 에 저장
|
|
log: (label, data) => send('log', label, data),
|
|
|
|
// 디버그 파일 전체 초기화
|
|
clear: async () => {
|
|
if (!isLoggingEnabled || typeof window === 'undefined') return
|
|
try {
|
|
await fetch('/api/debug', { method: 'DELETE' })
|
|
} catch {}
|
|
},
|
|
}
|