輸出信息到控制器。
查看log庫源文件可以了解更多詳細(xì)信息。
Grunt輸出看起來應(yīng)該是一致的,甚至是漂亮的。因此,這里有大量的日志記錄相關(guān)的方法和一些很有用的模式。所有的這些方法實際上記錄的東西的都是可連接的。
注意:所有grunt.verbose
下可用的方法都酷似grunt.log
方法,不同的是,如果指定--verbose
命令行選項,那么它就只會記錄日志信息。
輸出指定的msg
字符串信息,尾部不帶換行符。
grunt.log.write(msg);
輸出指定的msg
字符串信息,尾部帶有換行符。
grunt.log.writeln([msg]);
如果忽略了msg
字符串,它會輸出紅色的ERROR
信息;否則輸出>> msg
并且尾部帶有換行符。
grunt.log.error([msg])
使用grunt.log.error
會輸出一個錯誤日志信息,使用grunt.log.wraptext
可以做到每80個文本就換行(即保證輸出最大列數(shù)為80列)。
grunt.log/errorlns(msg)
如果忽略msg
字符串,它會輸出綠色的OK
信息;否則輸出>> msg
并且尾部帶有換行符。
grunt.log.ok([msg])
使用grunt.log.ok
方法輸出一個ok信息,使用grunt.log.wraptext
可以做到每80個文本就換行。
grunt.log.oklns(msg)
以加粗的形式輸出指定的msg
字符串,尾部帶有換行符。
grunt.log.subhead(msg)
輸出一個obj
屬性列表(它是很好的調(diào)試標(biāo)志)。
grunt.log.writeflags(obj, prefix)
輸出一個調(diào)試信息,但是只在指定--debug
命令行選項的情況下才會輸出。
grunt.log.debug(msg)
所有grunt.verbose
下可用的日志記錄方法的工作都酷似它們所對應(yīng)的grunt.log
方法,但是它們只在指定--verbose
命令行選項的情況下才一樣。還有一個對應(yīng)"notverbose"適用于grunt.log.notverbose
和grunt.log.verbose.or
。實際上,.or
屬性也可以用于在verbose
和notverbose
兩者之間有效的進行切換。
這個對象包含grunt.log
下的所有方法,但是只在指定--verbose
命令行選項情況下它才會輸出日志信息。
grunt.verbose
這個對象也包含grunt.log
下的所有方法,但是只在不指定--verbose
命令行選項情況下它才會輸出日志信息。
grunt.verbose.or
這些方法不會真正輸出日志信息,它們只返回可以用于其他方法中的字符串。
返回一個逗號分割的arr
數(shù)組項目。
grunt.log.wordlist(arr [, options])
options
對象可以使用以下屬性,并且還可以設(shè)置它們默認(rèn)值:
var options = {
// The separator string (can be colored).
separator: ', ',
// The array item color (specify false to not colorize).
color: 'cyan',
};
從字符串中移除所有的彩色信息,使它適用于測試.length
屬性或者可以輸出到一個日志記錄文件中。
grunt.log.uncolor(str)
將text
字符串包裝為width
指定寬度字符列并在尾部添加一個\n
(換行符),確保單詞沒有從中間分割,除非絕對必要的情況下才可以使用分割的單詞。
grunt.log.wraptext(width, text)
將texts
字符串?dāng)?shù)組包裝為widths
指定寬度的字符串列數(shù)。包裝函數(shù)grunt.log.wraptext
方法可以用于生成輸出列。
grunt.log.table(widths, texts)
一個常見的模式,發(fā)生錯誤時,它只在指定--verbose
mode OR選項的情況下輸出日志信息。就像下面這樣:
grunt.registerTask('something', 'Do something interesting.', function(arg) {
var msg = 'Doing something...';
grunt.verbose.write(msg);
try {
doSomethingThatThrowsAnExceptionOnError(arg);
// Success!
grunt.verbose.ok();
} catch(e) {
// Something went wrong.
grunt.verbose.or.write(msg).error().error(e.message);
grunt.fail.warn('Something went wrong.');
}
});
關(guān)于以上代碼的說明:
grunt.verbose.write(msg)
輸出指定的信息(沒有換行符),但是只在指定--verbose
選項的模式下才能正常工作;grunt.verbose.ok()
輸出綠色的OK信息,尾部帶有換行符;grunt.verbose.or.write(msg).error().error(e.message)
做了好幾件事情:
--verbose
選項的模式下則grunt.verbose.or.write(msg)
輸出信息(沒有換行符),并且它會返回`norverbos對象;.error()
輸出紅色的ERROR信息,尾部帶有換行符,它也會返回notverbose
對象;.error(e.message);
輸出實際的錯誤信息(同時返回notverbose
對象);grunt.fail.warn('Something went wrong.');
輸出一個淺黃色的警告信息,可以使用出口代碼1
退出Grunt,除非指定了--force
選項。查看grunt-contrib-*任務(wù)源碼可以看到更多的例子。