if (process.env.NODE_ENV !== 'production') {
logger.add(new winston.transports.Console({
format: winston.format.combine(
winston.format.colorize({ all: true }),
winston.format.simple()
)}));
}
上述代碼的含義是,如果目前 Node.js 執行環境不是生産環境,則将 winston 的輸出,列印到控制台上。
_stream_readable.js 抛出 data 事件:
讀取事件處理函數:
console 對應的 event handler:
transformed:
console 有專門對應的 transport 實作檔案,位于 console.js 内:
從 data 字元串能看出在 console 列印彩色字元串的實作方式:
并沒有像我想象的那樣,執行到 82 行的 console.log
console 對象的 _stdout 屬性,在 internal 這個 constructor.js 裡注入:
這個 writeUtf8String 函數,是原生 native API:
位于 Node.js 源代碼 的 internal/stream_base_common.js 内部:
writeUtf8String
stream_base_commons.js 檔案位置如下:
https://github.com/nodejs/node/tree/master/lib/internal
native 實作: