鍍金池/ 教程/ 物聯(lián)網(wǎng)/ grunt.log
深入任務(wù)內(nèi)幕
grunt.option
退出碼
創(chuàng)建插件
grunt.file
grunt
快速入門
項目腳手架
使用命令行工具
Gruntfile 實例
配置任務(wù)
創(chuàng)建任務(wù)
grunt.log
安裝Grunt
grunt.util
grunt.event
常見問題
grunt.config
grunt.task
grunt.template
grunt.fail

grunt.log

輸出信息到控制器。

查看log庫源文件可以了解更多詳細(xì)信息。

The log API

Grunt輸出看起來應(yīng)該是一致的,甚至是漂亮的。因此,這里有大量的日志記錄相關(guān)的方法和一些很有用的模式。所有的這些方法實際上記錄的東西的都是可連接的。

注意:所有grunt.verbose下可用的方法都酷似grunt.log方法,不同的是,如果指定--verbose命令行選項,那么它就只會記錄日志信息。

grunt.log.write/grunt.verbose.write

輸出指定的msg字符串信息,尾部不帶換行符。

grunt.log.write(msg);

grunt.log.weiteln/grunt.verbose.writeln

輸出指定的msg字符串信息,尾部帶有換行符。

grunt.log.writeln([msg]);

grunt.log.error/grunt.verbose.error

如果忽略了msg字符串,它會輸出紅色的ERROR信息;否則輸出>> msg并且尾部帶有換行符。

grunt.log.error([msg])

grunt.log.errorlns/grunt.verbose.errorlns

使用grunt.log.error會輸出一個錯誤日志信息,使用grunt.log.wraptext可以做到每80個文本就換行(即保證輸出最大列數(shù)為80列)。

grunt.log/errorlns(msg)

grunt.log.ok/grunt.verbose.ok

如果忽略msg字符串,它會輸出綠色的OK信息;否則輸出>> msg并且尾部帶有換行符。

grunt.log.ok([msg])

grunt.log.oklns/grunt.verbose.oklns

使用grunt.log.ok方法輸出一個ok信息,使用grunt.log.wraptext可以做到每80個文本就換行。

grunt.log.oklns(msg)

grunt.log.subhead/grunt.verbose.subhead

以加粗的形式輸出指定的msg字符串,尾部帶有換行符。

grunt.log.subhead(msg)

grunt.log.writeflags/grunt.verbose.writeflags

輸出一個obj屬性列表(它是很好的調(diào)試標(biāo)志)。

grunt.log.writeflags(obj, prefix)

grunt.log.debug/grunt.verbose.debug

輸出一個調(diào)試信息,但是只在指定--debug命令行選項的情況下才會輸出。

grunt.log.debug(msg)

Verbose and Notverbose

所有grunt.verbose下可用的日志記錄方法的工作都酷似它們所對應(yīng)的grunt.log方法,但是它們只在指定--verbose命令行選項的情況下才一樣。還有一個對應(yīng)"notverbose"適用于grunt.log.notverbosegrunt.log.verbose.or。實際上,.or屬性也可以用于在verbosenotverbose兩者之間有效的進行切換。

grunt.verbose/grunt.log.verbose

這個對象包含grunt.log下的所有方法,但是只在指定--verbose命令行選項情況下它才會輸出日志信息。

grunt.verbose

grunt.verbose.or/grunt.log.notverbose

這個對象也包含grunt.log下的所有方法,但是只在不指定--verbose命令行選項情況下它才會輸出日志信息。

grunt.verbose.or

工具方法

這些方法不會真正輸出日志信息,它們只返回可以用于其他方法中的字符串。

grunt.log.wordlist

返回一個逗號分割的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',
};

grunt.log.uncolor

從字符串中移除所有的彩色信息,使它適用于測試.length屬性或者可以輸出到一個日志記錄文件中。

grunt.log.uncolor(str)

grunt.log.wraptext

text字符串包裝為width指定寬度字符列并在尾部添加一個\n(換行符),確保單詞沒有從中間分割,除非絕對必要的情況下才可以使用分割的單詞。

grunt.log.wraptext(width, text)

grunt.log.table

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)于以上代碼的說明:

  1. grunt.verbose.write(msg) 輸出指定的信息(沒有換行符),但是只在指定--verbose選項的模式下才能正常工作;
  2. grunt.verbose.ok() 輸出綠色的OK信息,尾部帶有換行符;
  3. grunt.verbose.or.write(msg).error().error(e.message)做了好幾件事情:
    1. 如果不在指定--verbose選項的模式下則grunt.verbose.or.write(msg)輸出信息(沒有換行符),并且它會返回`norverbos對象;
    2. .error() 輸出紅色的ERROR信息,尾部帶有換行符,它也會返回notverbose對象;
    3. .error(e.message); 輸出實際的錯誤信息(同時返回notverbose對象);
  4. grunt.fail.warn('Something went wrong.'); 輸出一個淺黃色的警告信息,可以使用出口代碼1退出Grunt,除非指定了--force選項。

查看grunt-contrib-*任務(wù)源碼可以看到更多的例子。

上一篇:grunt下一篇:grunt.option