grunt.log
メッセージをコンソール上に出力します。 詳しくは、ログライブラリのソース を参照してください。
Gruntのログ出力は一貫性があり、もしかしたら小奇麗(?)でさえあるかもしれません。 多くのログメソッドといくつかの有用なパターンがあります。 実際にログを残す全てのメソッドは、チェーン可能です。
grunt.verbose下で利用可能な全てのメソッドは、まるでgrunt.logであるかのように動作しますが、
コマンドライン上で--verboseコマンドが指定された場合のみ、ログ出力されます。
- grunt.log.write / grunt.verbose.write
- grunt.writeln / grunt.verbose.writeln
- grunt.log.error / grunt.verbose.error
- grunt.log.errorlns / grunt.verbose.errorlns
- grunt.log.ok / grunt.verbose.ok
- grunt.log.oklns / grunt.verbose.oklns
- grunt.log.subhead / grunt.verbose.subhead
- grunt.log.writeflags / grunt.verbose.writeflags
- grunt.log.debug / grunt.verbose.debug
- grunt.verbose / grunt.log.verbose
- grunt.verbose.or / grunt.log.notverbose
- grunt.log.wordlist
- grunt.log.uncolor
- grunt.log.wraptext
- grunt.log.table
grunt.log.write / grunt.verbose.write
指定されたメッセージを改行無しでログ出力します。
grunt.log.write(msg)
grunt.writeln / grunt.verbose.writeln
指定されたメッセージを改行付きでログ出力します。
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としてログ出力したテキストを、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が指定された場合のみです。
"notverbose"も、grunt.log.notverboseと grunt.log.verbose.orの両方を利用することが出来ます。
実際には、.orプロパティはverboseとnotverboseの両方を使用することができ、
効果的にこの2つを切り替えます。
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])
このオプションオブジェクトで使用出来るプロパティと初期値は下記のとおりです。
var options = {
// 区切り文字(色付け可能)
separator: ', ',
// 配列項目を色付けします(falseを指定すると色付けしません)。
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);
// 成功!
grunt.verbose.ok();
} catch(e) {
// 何か問題が発生している
grunt.verbose.or.write(msg).error().error(e.message);
grunt.fail.warn('Something went wrong.');
}
});
下記にこのコードについて説明します。
grunt.verbose.write(msg);が、メッセージ(改行無し)をログ出力します。ただし、--verbose指定された場合のみに限ります。grunt.verbose.ok();が、緑文字でOKを改行付きでログ出力します。grunt.verbose.or.write(msg).error().error(e.message);は、いくつかの事を行います。-
grunt.verbose.or.write(msg)が、--verboseの指定が無ければ、メッセージ(改行無し)をログ出力し、notverboseオブジェクトを返します。 -
.error()が、赤文字でERRORを改行付きでログ出力し、notverboseオブジェクトを返します。 -
.error(e.message); logs the actual error message (and returns the notverbose object).
.error(e.message);が、実際のエラーメッセージをログ出力します。 (また、notverboseオブジェクトを返します。) -
grunt.fail.warn('Something went wrong.');が、黄文字で警告をログ出力し、--forceの指定が無ければ、exitコード1でGruntを終了します。
© 2010 - 2017 STUDIO KINGDOM