May 25, 2021 Grunt
Output information to the console.
See log lib source for more information.
Grunt's output should look consistent and beautiful. T herefore, there are these log methods and some useful patterns. All methods used to output logs can be called chained.
Note: All methods in
grunt.verbose
when the
--verbose
option is specified on the command line and work exactly like .log in
grunt.log
Outputs
msg
string log, ending without a newline.
grunt.log.write(msg)
Outputs
msg
string log with a line break at the end.
grunt.log.writeln([msg])
If the
msg
omitted,
>> msg
ERROR
is output in red font, otherwise it is output with a line break at the tail.
grunt.log.error([msg])
Use
grunt.log.error
to log an error
grunt.log.wraptext
output the log as 80 characters per line.
grunt.log.errorlns(msg)
If the
msg
omitted,
>> msg
OK
is output in a green font, otherwise it is output and has a line break at the tail.
grunt.log.ok([msg])
Use
grunt.log.ok
record an ok message, and
grunt.log.wraptext
output logs as 80 characters per line.
grunt.log.oklns(msg)
Record the
msg
string and bold
it with a line break at the end.
grunt.log.subhead(msg)
Record
obj
(best used to debug flags).
grunt.log.writeflags(obj, prefix)
A debug message is logged, but is output only if the
--debug
on the command line.
grunt.log.debug(msg)
All
grunt.verbose
work much like the
grunt.log
they correspond to, but they only work if you specify
--verbose
command line option. T
here is also a corresponding "notverbose" for
grunt.log.notverbose
grunt.log.verbose.or
In fact,
.or
property can also be used to effectively switch
verbose
and
notverbose
This object
grunt.log
but it outputs log information only if the
--verbose
command line option is specified.
grunt.verbose
This object also
grunt.log
but it outputs log information only if
--verbose
command line option is not specified.
grunt.verbose.or
These methods don't actually log, they only return strings, and the strings returned can be used for other methods.
Returns a comma-separated list of
arr
array items.
arr
in the arr array are returned as comma segments.
grunt.log.wordlist(arr [, options])
options
have the following properties and defaults:
var options = {
// The separator string (can be colored).
separator: ', ',
// The array item color (specify false to not colorize).
color: 'cyan',
};
Remove all color information from the string to make it suitable for detecting
.length
or writing to log files.
、
grunt.log.uncolor(str)
Breaking
width
string as a set of width
text
and adding the
\n
of . . . will try to ensure that the word is not truncated from the middle unless absolutely necessary.
grunt.log.wraptext(width, text)
The
width
string is broken down as a
text
characters. W
rap
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)
The usual pattern is to output
--verbose
mode or when an error occurs, as follows:
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.');
}
});
Explain the above code:
grunt.verbose.write(msg);
Log information (no line breaks) is output only in
--verbose
mode.
grunt.verbose.ok();
Output log information in green and line breaks at the end.
grunt.verbose.or.write(msg).error().error(e.message);
Here are a few things you can do:
grunt.verbose.or.write(msg)
log information if it is not in
--verbose
mode, and
notverbose
object.
.error()
the ERROR log in red, ends with a line break, and returns
notverbose
object.
.error(e.message);
Output the actual error message (and return
notverbose
object).
grunt.fail.warn('Something went wrong.');
Output the warning message in tender yellow.
Output exit code 1
--force
unless the --force option is specified on the command line.
Check the source code for the grunt-contrib-?task for more cases.