Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

grunt.log


May 25, 2021 Grunt


Table of contents


grunt.log

Output information to the console.

See log lib source for more information.

The log API

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

grunt.log.write / grunt.verbose.write

Outputs msg string log, ending without a newline.

grunt.log.write(msg)

grunt.log.writeln / grunt.verbose.writeln

Outputs msg string log with a line break at the end.

grunt.log.writeln([msg])

grunt.log.error / grunt.verbose.error

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])

grunt.log.errorlns / grunt.verbose.errorlns

Use grunt.log.error to log an error grunt.log.wraptext output the log as 80 characters per line.

grunt.log.errorlns(msg)

grunt.log.ok / grunt.verbose.ok

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])

grunt.log.oklns / grunt.verbose.oklns

Use grunt.log.ok record an ok message, and grunt.log.wraptext output logs as 80 characters per line.

grunt.log.oklns(msg)

grunt.log.subhead / grunt.verbose.subhead

Record the msg string and bold it with a line break at the end.

grunt.log.subhead(msg)

grunt.log.writeflags / grunt.verbose.writeflags

Record obj (best used to debug flags).

grunt.log.writeflags(obj, prefix)

grunt.log.debug / grunt.verbose.debug

A debug message is logged, but is output only if the --debug on the command line.

grunt.log.debug(msg)

Verbose and Notverbose

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

grunt.verbose / grunt.log.verbose

This object grunt.log but it outputs log information only if the --verbose command line option is specified.

grunt.verbose

grunt.verbose.or / grunt.log.notverbose

This object also grunt.log but it outputs log information only if --verbose command line option is not specified.

grunt.verbose.or

Tool method

These methods don't actually log, they only return strings, and the strings returned can be used for other methods.

grunt.log.wordlist

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',
};

grunt.log.uncolor

Remove all color information from the string to make it suitable for detecting .length or writing to log files.

grunt.log.uncolor(str)

grunt.log.wraptext

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)

grunt.log.table

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)

Case

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:

  1. grunt.verbose.write(msg); Log information (no line breaks) is output only in --verbose mode.
  2. grunt.verbose.ok(); Output log information in green and line breaks at the end.
  3. grunt.verbose.or.write(msg).error().error(e.message); Here are a few things you can do:
    1. grunt.verbose.or.write(msg) log information if it is not in --verbose mode, and notverbose object.
    2. .error() the ERROR log in red, ends with a line break, and returns notverbose object.
    3. .error(e.message); Output the actual error message (and return notverbose object).
  4. 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.