沒有 DevTools 的年代,除錯只能靠 alert。即使今天有完整的開發者工具,「把日誌寫到畫面上」在某些情境(行動裝置、客戶現場、無法開 console 的環境)仍然很實用。這裡介紹一個小型的除錯包 jscript.debug.js,它只提供兩件事:列舉物件屬性,以及一個分級的日誌輸出器。
/**
* jscript.debug package
* This package contains utility functions for helping debug JavaScript.
*
*/
/*命名空間*/
if (typeof jscript == 'undefined') {
jscript = function() { }
}
jscript.debug = function() { }
最實用的小工具之一:傳入一個物件,就弹出它所有的屬性名稱與對應值。
jscript.debug.enumProps = function(inObj) {
var props = "";
var i;
for (i in inObj) {
props += i + " = " + inObj[i] + "\n";
}
alert(props);
} // End enumProps().
現代寫法可以直接用 console.log(obj) 或 Object.entries(obj),但注意 for...in 會連原型鏈上的可列舉屬性一起列出,必要時應加 hasOwnProperty 過濾。
這個類別的設計重點是分級:定義六個層級,各自對應一種顏色,並可設定「最低顯示層級」,低於門檻的訊息直接丟棄。這樣在正式環境只要把門檻調高,就不會有雜訊干擾。
this.LEVEL_TRACE = 1; this.LEVEL_DEBUG = 2; this.LEVEL_INFO = 3; this.LEVEL_WARN = 4; this.LEVEL_ERROR = 5; this.LEVEL_FATAL = 6; this.LEVEL_TRACE_COLOR = "a0a000"; this.LEVEL_DEBUG_COLOR = "64c864"; this.LEVEL_INFO_COLOR = "000000"; this.LEVEL_WARN_COLOR = "0000ff"; this.LEVEL_ERROR_COLOR = "ff8c00"; this.LEVEL_FATAL_COLOR = "ff0000"; //需要顯示的最小錯誤級別,默認為 3 this.logLevel = 3; //targetDiv is the DIV object to output to. this.targetDiv = null;
this.setLevel = function(inLevel) {
this.logLevel = inLevel;
} // End setLevel().
this.setTargetDiv = function(inTargetDiv) {
this.targetDiv = inTargetDiv;
this.targetDiv.innerHTML = "";
} // End setTargetDiv().
this.shouldBeLogged = function(inLevel) {
if (inLevel >= this.logLevel) {
return true;
} else {
return false;
}
} // End shouldBeLogged().
結構完全一致,只是層級與顏色不同,因此這裡以 trace 與 fatal 為代表:
this.trace = function(inMessage) {
if (this.shouldBeLogged(this.LEVEL_TRACE) && this.targetDiv) {
this.targetDiv.innerHTML +=
"<div style='color:#" + this.LEVEL_TRACE_COLOR + ";'>" +
"[TRACE] " + inMessage + "</div>";
}
} // End trace().
this.fatal = function(inMessage) {
if (this.shouldBeLogged(this.LEVEL_FATAL) && this.targetDiv) {
this.targetDiv.innerHTML +=
"<div style='color:#" + this.LEVEL_FATAL_COLOR + ";'>" +
"[FATAL] " + inMessage + "</div>";
}
} // End fatal().
其餘 debug、info、warn、error 依同樣模式撰寫即可。
<div id="jscript_debug_div" style="font-family:arial; font-size:10pt; font-weight:bold; display:none; background-color:#ffffe0; padding:4px;">
<a href="javascript:void(0);" id="enumPropsLink"
onClick="jscript.debug.enumProps(document.getElementById('enumPropsLink'));">
enumProps()-Shows all the properties of this link
</a>
<br><br>
<div id="divLog" style="font-family:arial; font-size: 12pt; padding: 4px; background-color:#ffffff; border:1px solid #000000; width:50%; height:300px; overflow:scroll;">Log message will appear here</div>
<script>
var log = new jscript.debug.DivLogger();
log.setTargetDiv(document.getElementById("divLog"));
log.setLevel(log.LEVEL_DEBUG);
</script>
三行初始化設定了輸出目標與門檻(LEVEL_DEBUG),之後各處只要呼叫 log.debug(...)、log.error(...) 即可。因為門檻是 DEBUG,低一級的 trace 訊息不會顯示。
點擊第一個連結時,瀏覽器會弹出目前這個 <a> 元素的所有屬性與值(以 Opera 為例),這在沒有 console 時特別有用。
分級日誌的想法完全沒有過時,它就是今天所有正式日誌框架(log4j、winston、pino)的核心概念。若要在現代專案裡沿用類似做法,合理的調整是:用 console.debug/info/warn/error 取代直接操作 innerHTML(避免拼接 HTML 造成 XSS 風險,也避免每次輸出都觸發重排),並透過環境變數或 localStorage 旗標動態調整門檻,如此正式環境就能安靜、除錯時又能一秒切回詳細模式。