7

在我的 Gruntfile 中,如何将日志语句添加到其处理中,如下例所示?

 karma: {
        unit: {
            configFile: "<%= grunt.option('Debug') ? 'build/karma.conf.js' : '' %>",
            console.log(configFile),
            singleRun: true,
            browsers: ['PhantomJS']
        },
    }
4

4 回答 4

9

Gruntfiles 是 javascript,因此console.log()只要它是有效的 javascript,您就可以在任何地方使用。

grunt.initConfig({
  karma: {
    unit: {
      configFile: 'build/karma.conf.js'
    }
  }
});
if (grunt.option('debug')) {
  console.log(grunt.config('karma.unit.configFile'));
}
于 2013-09-25T23:03:30.113 回答
3

我不是您要问的,但是如果您想将调试日志记录放在 Gruntfile.js 中,您见过grunt.log方法吗?

于 2014-07-14T10:40:36.730 回答
2

It would be nice if it were that easy... console.log() only outputs client-side stuff to the client; however, since you're working on the server-side of things, you won't see anything pop up in the browser console (rather the server console, probably your terminal).

There is a way around this thanks to the work of others, for instance: https://github.com/ethanl/connect-browser-logger

This will basically hoist those server side logs out to the client for you to see. If you do a Google, you'll find a slew of other solutions (some with the ability to set breakpoints, step through code, etc).

Not shabby!

Edit: Christ, I just realized you wanted the logging specifically IN your gruntfile. That's a bit of a different story, but it still should work for you!

于 2013-09-25T16:23:13.330 回答
0

有各种工具,例如节点检查器,可以调试这些特定文件。

在节点检查器上(来自 github 页面):

Node Inspector 是 Node.js 应用程序的调试器接口,它使用 Blink 开发工具(以前称为 WebKit Web Inspector)。

这个stackoverflow问题有一些关于如何具体做到这一点的很好的答案: Using node-inspector with Grunt tasks

于 2014-08-26T14:35:03.470 回答