Files
calculate-version/app/logger.js
T

82 lines
3.2 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use strict';
// 主分隔線與次分隔線寬度(沿用原 entrypoint.sh 的視覺樣式)
const LINE = '='.repeat(50);
const SUBLINE = '-'.repeat(50);
/**
* 產生台灣時區(Asia/Taipei)的時間戳,格式固定為 `yyyy/MM/dd HH:mm:ss`。
*
* 供 log 訊息統一格式 `[{等級}][{時間}]: {訊息}` 的時間欄位使用。
*
* @returns {string} 台灣時區當下時間字串,例如 `2026/06/30 12:13:04`。
*/
function taipeiTimestamp() {
const parts = new Intl.DateTimeFormat('zh-TW', {
timeZone: 'Asia/Taipei',
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false,
}).formatToParts(new Date());
const get = (type) => parts.find((part) => part.type === type)?.value ?? '';
// 部分執行環境會把 24 時制的午夜輸出為 "24",統一正規化為 "00"
const hour = get('hour') === '24' ? '00' : get('hour');
return `${get('year')}/${get('month')}/${get('day')} ${hour}:${get('minute')}:${get('second')}`;
}
/**
* 輸出帶標題的區塊段落至標準輸出,標題前後以分隔線包夾,
* 用於在 log 中建立可視的段落區隔。
*
* 輸出格式為:換行 + 主分隔線(50 個 `=`)+ 標題 + 次分隔線(50 個 `-`)。
* 此為結構性段落標題(非 INF/WRN/ERR 等級訊息),故不套用 `[{等級}][{時間}]` 前綴。
*
* @param {string} title - 區塊標題文字;會原樣輸出於兩條分隔線之間。
* @returns {void}
* @remarks 通常在進入一個處理階段前呼叫(例如「參數檢查」「取得版本資料」),
* 用以在 CI/容器 log 中分隔各階段輸出,便於閱讀與定位。
*/
function section(title) {
process.stdout.write(`\n${LINE}\n${title}\n${SUBLINE}\n`);
}
/**
* 輸出一般資訊(INF)層級的 log 訊息至標準輸出。
*
* 訊息格式統一為 `[INF][{台灣時間}]: {訊息}`,時間使用台灣時區(Asia/Taipei),
* 格式為 `yyyy/MM/dd HH:mm:ss`,並於結尾換行。
*
* @param {string} message - 要輸出的資訊內容。
* @returns {void}
* @remarks 用於回報正常流程進度(如設定值、URL、各頁取得筆數);此調整僅變更輸出前綴格式,
* 不改變訊息所反映的實際行為與內容。
*/
function info(message) {
process.stdout.write(`[INF][${taipeiTimestamp()}]: ${message}\n`);
}
/**
* 輸出錯誤(ERR)層級的 log 訊息至標準錯誤輸出(stderr)。
*
* 訊息格式統一為 `[ERR][{台灣時間}]: {訊息}`,時間使用台灣時區(Asia/Taipei),
* 格式為 `yyyy/MM/dd HH:mm:ss`,並於結尾換行。
*
* 僅負責輸出,不終止行程;是否結束由呼叫端(進入點)決定,以利測試與錯誤復原。
*
* @param {string} message - 要輸出的錯誤描述內容。
* @returns {void}
* @remarks 由進入點在頂層捕捉到例外時呼叫,輸出至 stderr 後再由呼叫端決定 exit code
* 此調整僅變更輸出前綴格式,不改變訊息所反映的實際行為與內容。
*/
function error(message) {
process.stderr.write(`[ERR][${taipeiTimestamp()}]: ${message}\n`);
}
module.exports = { section, info, error };