Output messages to the console.
See the log lib source for more information.
Grunt output should look consistent, and maybe even pretty. As such, there is a plethora of logging methods, and a few useful patterns. All of the methods that actually log something are chainable.
Note: all methods available under grunt.verbose work exactly like grunt.log methods, but only log if the --verbose command-line option was specified.
Log the specified msg string, with no trailing newline.
grunt.log.write(msg)
Log the specified msg string, with trailing newline.
grunt.log.writeln([msg])
If msg string is omitted, logs ERROR in red, otherwise logs >> msg, with trailing newline.
grunt.log.error([msg])
Log an error with grunt.log.error, wrapping text to 80 columns using grunt.log.wraptext.
grunt.log.errorlns(msg)
If msg string is omitted, logs OK in green, otherwise logs >> msg, with trailing newline.
grunt.log.ok([msg])
Log an ok message with grunt.log.ok, wrapping text to 80 columns using grunt.log.wraptext.
grunt.log.oklns(msg)
Log the specified msg string in bold, with trailing newline.
grunt.log.subhead(msg)
Log a list of obj properties (good for debugging flags).
grunt.log.writeflags(obj, prefix)
Logs a debugging message, but only if the --debug command-line option was specified.
grunt.log.debug(msg)
All logging methods available under grunt.verbose work exactly like their grunt.log counterparts, but only log if the --verbose command-line option was specified. There is also a "notverbose" counterpart available at both grunt.log.notverbose and grunt.log.verbose.or. In fact, the .or property can be used on both verbose and notverbose to effectively toggle between the two.
This object contains all methods of grunt.log but only logs if the --verbose command-line option was specified.
grunt.verbose
This object contains all methods of grunt.log but only logs if the --verbose command-line option was not specified.
grunt.verbose.or
These methods don't actually log, they just return strings that can be used in other methods.
Returns a comma-separated list of arr array items.
grunt.log.wordlist(arr [, options])
The options object has these possible properties, and default values:
var options = {
// The separator string (can be colored).
separator: ', ',
// The array item color (specify false to not colorize).
color: 'cyan',
}; Removes all color information from a string, making it suitable for testing .length or perhaps logging to a file.
grunt.log.uncolor(str)
Wrap text string to width characters with \n, ensuring that words are not split in the middle unless absolutely necessary.
grunt.log.wraptext(width, text)
Wrap texts array of strings to columns widths characters wide. A wrapper for the grunt.log.wraptext method that can be used to generate output in columns.
grunt.log.table(widths, texts)
A common pattern is to only log when in --verbose mode OR if an error occurs, like so:
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.');
}
}); An explanation of the above code:
grunt.verbose.write(msg); logs the message (no newline), but only in --verbose mode.grunt.verbose.ok(); logs OK in green, with a newline.grunt.verbose.or.write(msg).error().error(e.message); does a few things:grunt.verbose.or.write(msg) logs the message (no newline) if not in --verbose mode, and returns the notverbose object..error() logs ERROR in red, with a newline, and returns the notverbose object..error(e.message); logs the actual error message (and returns the notverbose object).grunt.fail.warn('Something went wrong.'); logs a warning in bright yellow, exiting Grunt with exit code 1, unless --force was specified.Take a look at the grunt-contrib-* tasks source code for more examples.
© GruntJS Team
Licensed under the MIT License.
https://gruntjs.com/api/grunt.log