fix(src/index): 共用 log 格式化流程

This commit is contained in:
2026-07-11 10:08:52 +00:00
parent c60b71e987
commit 768ab84b00
+30 -12
View File
@@ -17,6 +17,34 @@ function section(title) {
currentStage = title;
}
/**
* 產生統一格式的時間戳記字串。
*
* @returns {string} Asia/Taipei 時區格式化後的時間戳記。
* @remarks
* 由 `info()` 與錯誤輸出共用,避免不同輸出路徑各自拼出不同格式。
*/
function formatTimestamp() {
return new Date()
.toLocaleString('sv-SE', { timeZone: 'Asia/Taipei', hour12: false })
.replace(/-/g, '/');
}
/**
* 以統一格式組裝單行 log。
*
* @param {string} level 訊息等級。
* @param {string} message 要輸出的訊息內容。
* @returns {string} 已格式化完成且含換行的 log 字串。
* @remarks
* 讓一般資訊與錯誤訊息共用同一套前綴格式,降低未來調整格式時的維護成本。
*/
function formatLogLine(level, message) {
const stagePrefix = currentStage ? `[${currentStage}]` : '';
return `${stagePrefix}[${level}][${formatTimestamp()}]: ${message}\n`;
}
/**
* 以統一格式輸出資訊訊息。
*
@@ -25,12 +53,7 @@ function section(title) {
* 會附加目前階段與 Asia/Taipei 時區時間戳記,適合用於流程進度與狀態輸出。
*/
function info(message) {
const timestamp = new Date()
.toLocaleString('sv-SE', { timeZone: 'Asia/Taipei', hour12: false })
.replace(/-/g, '/');
const stagePrefix = currentStage ? `[${currentStage}]` : '';
process.stdout.write(`${stagePrefix}[INF][${timestamp}]: ${message}\n`);
process.stdout.write(formatLogLine('INF', message));
}
/**
@@ -398,12 +421,7 @@ async function main() {
if (require.main === module) {
main().catch((error) => {
const timestamp = new Date()
.toLocaleString('sv-SE', { timeZone: 'Asia/Taipei', hour12: false })
.replace(/-/g, '/');
const stagePrefix = currentStage ? `[${currentStage}]` : '';
process.stderr.write(`${stagePrefix}[ERR][${timestamp}]: ${error.message}\n`);
process.stderr.write(formatLogLine('ERR', error.message));
process.exit(1);
});
}