黑人生命也是命

grunt.log

輸出訊息至主控台。

請參閱 log lib 來源 以取得更多資訊。

記錄 API

Grunt 輸出應看起來一致,甚至美觀。因此,有許多記錄方法和一些有用的模式。實際記錄某些內容的所有方法都是可串連的。

注意:在 grunt.verbose 下可用的所有方法都與 grunt.log 方法完全相同,但僅在指定 --verbose 命令列選項時才記錄。

grunt.log.write / grunt.verbose.write

記錄指定的 msg 字串,沒有尾隨換行符。

grunt.log.write(msg)

grunt.log.writeln / 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.wraptext 將文字換行至 80 個欄位,記錄一個 grunt.log.error 錯誤。

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.wraptext 將文字換行至 80 個欄位,記錄一個 grunt.log.ok 正常訊息。

grunt.log.oklns(msg)

grunt.log.subhead / grunt.verbose.subhead

粗體 記錄指定的 msg 字串,有尾隨換行符。

grunt.log.subhead(msg)

grunt.log.writeflags / grunt.verbose.writeflags

記錄 obj 屬性的清單(適合用於偵錯旗標)。

grunt.log.writeflags(obj, prefix)

grunt.log.debug / grunt.verbose.debug

記錄一個偵錯訊息,但僅在指定 --debug 命令列選項時才記錄。

grunt.log.debug(msg)

Verbose 和 Notverbose

grunt.verbose 下可用的所有記錄方法,其運作方式與 grunt.log 對應的方法完全相同,但僅在指定 --verbose 命令列選項時才會記錄。在 grunt.log.notverbosegrunt.log.verbose.or 中也有可用的「notverbose」對應方法。事實上,.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 陣列項目以逗號分隔的清單。

grunt.log.wordlist(arr [, options])

options 物件具有下列可能的屬性,以及預設值

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 字串以 \n 換行,換行至 width 個字元,並確保不會將字詞拆開,除非絕對必要。

grunt.log.wraptext(width, text)

grunt.log.table

texts 字串陣列換行至寬度為 widths 個字元的欄位。grunt.log.wraptext 方法的包裝器,可用於在欄位中產生輸出。

grunt.log.table(widths, texts)

範例

常見的模式是在 --verbose 模式下才記錄,或是在發生錯誤時記錄,如下所示

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.');
  }
});

上述程式碼的說明

  1. grunt.verbose.write(msg); 記錄訊息(無換行),但僅在 --verbose 模式下。
  2. grunt.verbose.ok(); 以綠色記錄 OK,並換行。
  3. grunt.verbose.or.write(msg).error().error(e.message); 會執行一些動作
    1. grunt.verbose.or.write(msg) 如果不在 --verbose 模式下,會記錄訊息(沒有換行),並傳回 notverbose 物件。
    2. .error() 會以紅色記錄 ERROR,並換行,並傳回 notverbose 物件。
    3. .error(e.message); 會記錄實際的錯誤訊息(並傳回 notverbose 物件)。
  4. grunt.fail.warn('Something went wrong.'); 會以亮黃色記錄警告,並以結束代碼 1 結束 Grunt,除非指定了 --force

請參閱 grunt-contrib-* 任務原始碼 以取得更多範例。