在調(diào)試js代碼的時(shí)候,為調(diào)試的日志添加樣式可以使信息更醒目。
在chrome允許你控制日志消息的樣式。
例:
console.log('%c this is a message','color:#0f0;')
console.log('%c this %c is a %c message','color:#f00;','font-size:20px;','color:blue;background:yellow;')
第一個(gè)參數(shù)就是要輸出的字符串,通過%c分割的區(qū)間與之后的參數(shù)一一對(duì)應(yīng),參數(shù)就是標(biāo)準(zhǔn)的css,如果對(duì)應(yīng)的參數(shù)不足,無(wú)法匹配%c會(huì)以字符串的形式輸出,參數(shù)過多就會(huì)直接以字符串形式輸出多余的樣式。

20170728182626.png
然而在shell中以上的方法就不行了,在node服務(wù)中我們可以使用colors模塊來(lái)控制樣式
安裝
npm install colors
使用方法非常簡(jiǎn)便,模塊通過擴(kuò)展 String.prototype 添加樣式,而且支持鏈?zhǔn)秸{(diào)用。
var colors = require('colors');
console.log('hello'.green); // outputs green text
console.log('i like cake and pies'.underline.red) // outputs red underlined text
console.log('inverse the color'.inverse); // inverses the color
console.log('OMG Rainbows!'.rainbow); // rainbow
console.log('Run the trap'.trap); // Drops the bass

colors.png
了解更多colors