grunt.log

向控制台输出消息。

有关更多信息,请查看 log 库源代码

日志 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.error 记录错误,使用 grunt.log.wraptext 将文本换行到 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 属性的列表(适用于调试标志)。

grunt.log.writeflags(obj, prefix)

grunt.log.debug / grunt.verbose.debug

记录调试消息,但仅在指定了 --debug 命令行选项时。

grunt.log.debug(msg)

详细和非详细模式

grunt.verbose 下可用的所有日志记录方法的工作方式与其 grunt.log 对应方法完全相同,但仅在指定了 --verbose 命令行选项时才记录日志。在 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 数组项的逗号分隔列表。

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

options 对象具有以下可能的属性和默认值:

var options = {
  // 分隔符字符串(可以是彩色的)。
  separator: ', ',
  // 数组项颜色(指定 false 表示不着色)。
  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 字符串数组换行到 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.'); 以明亮的黄色记录警告,除非指定了 --force,否则以退出代码 1 退出 Grunt。

查看 grunt-contrib-* 任务源代码 以获取更多示例。