bada-terminal / src /utils /logger.js
VanyaJ's picture
Release BADA Terminal v2.1.0 (Enterprise Ontology, NVIDIA auto-ontology, Release Preflight)
d739b1b verified
Raw History Blame Contribute Delete
2.18 kB
const { writeDebugLog } = require('./debug-log');
// ๊ฒฝ๋Ÿ‰ ๊ตฌ์กฐํ™” ๋กœ๊ฑฐ.
// troubleshooting_guide.md 4.2 ๊ถŒ์žฅ ํ˜•์‹: { message, errorName, stack, context, timestamp }
// - ์ฝ˜์†”์—๋Š” ์‚ฌ๋žŒ์ด ์ฝ๊ธฐ ์‰ฌ์šด ํ•œ ์ค„ ์š”์•ฝ์„ ์ถœ๋ ฅ
// - ๋””๋ฒ„๊ทธ ๋กœ๊ทธ ํŒŒ์ผ(desktop-cli-debug.log)์—๋Š” JSON ํ•œ ์ค„์„ ๊ธฐ๋ก (writeDebugLog ๊ฒฝ์œ , ๋กœํ…Œ์ด์…˜ ํฌํ•จ)
const LEVELS = { debug: 10, info: 20, warn: 30, error: 40 };
function serializeError(err) {
if (!err) return null;
if (err instanceof Error) {
return { name: err.name, message: err.message, stack: err.stack };
}
// ๋ฌธ์ž์—ด/๊ฐ์ฒด ๋“ฑ ๋น„-Error ๊ฐ’๋„ ํก์ˆ˜
return { name: 'NonError', message: String(err.message || err), stack: null };
}
function emit(level, scope, message, context, err) {
const error = serializeError(err);
const record = {
level,
scope: scope || 'app',
message,
errorName: error ? error.name : undefined,
stack: error ? error.stack : undefined,
context: context && Object.keys(context).length ? context : undefined,
timestamp: new Date().toISOString(),
};
// ์ฝ˜์†” ์š”์•ฝ (๊ฐœ๋ฐœ ์ค‘ ๊ฐ€์‹œ์„ฑ)
const summary = `[${record.scope}] ${message}` + (error ? ` :: ${error.name}: ${error.message}` : '');
if (LEVELS[level] >= LEVELS.error) console.error(summary);
else if (LEVELS[level] >= LEVELS.warn) console.warn(summary);
else if (process.env.BADA_LOG_ALL || process.env.NODE_ENV === 'test') console.log(summary);
// ๊ตฌ์กฐํ™” JSON ํ•œ ์ค„ (ํŒŒ์ผ). writeDebugLog๋Š” ์‹คํŒจํ•ด๋„ ์ž์ฒด fallback ์ฒ˜๋ฆฌ.
try {
writeDebugLog(JSON.stringify(record));
} catch (_) {
// ๋กœ๊น… ์ž์ฒด ์‹คํŒจ๋Š” ๋ฌด์‹œ (๋กœ๊น…์œผ๋กœ ์•ฑ ํ๋ฆ„์„ ๊นจ์ง€ ์•Š์Œ)
}
}
function makeLogger(scope) {
return {
debug: (message, context) => emit('debug', scope, message, context, null),
info: (message, context) => emit('info', scope, message, context, null),
warn: (message, context, err) => emit('warn', scope, message, context, err),
error: (message, err, context) => emit('error', scope, message, context, err),
};
}
module.exports = {
makeLogger,
// ์Šค์ฝ”ํ”„ ์—†๋Š” ์ง์ ‘ ํ˜ธ์ถœ์šฉ
...makeLogger('app'),
};