logfile-grunt

Simple text file logging for Grunt and task output.

logfile-grunt

Simple text file logging for Grunt and task output.

This plugin requires Grunt ~0.4.0

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install logfile-grunt --save-dev

Once the plugin has been installed, the simplest way to enable it inside your Gruntfile is with this line of JavaScript at the top of your Gruntfile:

require('logfile-grunt')(grunt);

Grunt output and task information that is usually logged to console will also be logged file. Don't worry, all your console information will still be available, it will additionally be written to file as well.

The plugin can take an options object to enable you to override things like the path of the log file and whether to clear it each time you run Grunt.

Type: String Default value: './logs/grunt.log'

A file name (and path) where your log file will be created.

Type: Boolean Default value: false

Setting clearLogFile to true will ensure the log file emptied each time you run this task. Setting clearLogFile to false will continue to append to the same log file each time Grunt is executed.

Type: Boolean Default value: false

Setting keepColors to true will retain the console color codes and write them to the log file. Note that the console color codes can often make the text log difficult to read.

In this example with no options, all the output you see in the console from both Grunt and running tasks will also be written to ./logs/grunt.log. The log file text will have console color codes stripped out by default as well.

require('logfile-grunt')(grunt);

In this example, you can provide a custom file path for your log file by providing a name and path in the options object.

require('logfile-grunt')(grunt, { filePath: './logs/MyCustomLogs.txt' });

In this example, the custom log file will be cleared on every time Grunt is executed. This is useful for release build log files for example.

require('logfile-grunt')(grunt, { filePath: './logs/ClearedOnEveryRun.log', clearLogFile: true });

In this example, the log file will retain the console color codes used by Grunt.

require('logfile-grunt')(grunt, { keepColors: true });

The normal usage would be to require the plugin at the beginning of your Gruntfile so that all output will be logged no matter what task you run. If you need to send output to different log files depending on the task, you will need to start the logger inside a taskFunction which can be provided when you register a task.

var logfile = require('logfile-grunt');
 
grunt.task.registerTask('devlog', 'Keep appending everything to a log file.', function() {
  logfile(grunt, { filePath: './logs/MyDevLog.txt', clearLogFile: false });
});
 
grunt.task.registerTask('buildlog', 'Create a new release build log files on each run.', function() {
  logfile(grunt, { filePath: './dist/build.log', clearLogFile: true });
});
 
// Then include these tasks inside other tasks. 
// Make sure it's the first one so that all output is written to the log file. 
task.registerTask('default', ['devlog', 'jshint', 'qunit', 'concat', 'uglify']);
task.registerTask('dist', ['buildlog', 'concat:dist', 'uglify:dist']);

Concurrent plugins such as grunt-concurrent and grunt-parallelize spawn new tasks and can also alter the stdout and stderr streams. To ensure logging is reliable and all output spawned tasks is also logged, require logfile-grunt outside of your module export and start the logger inside the tasks that trigger concurrent tasks.

var logfile = require('logfile-grunt');
 
module.exports = function (grunt) {
  grunt.loadNpmTasks('grunt-concurrent');
 
  grunt.registerTask('log', 'Log to the console.', function (message) {
    console.log(message);
  });
 
  grunt.registerTask('default', 'Log to the console.', function () {
    // Start logging before any concurrent tasks are created. 
    logfile(grunt);
    grunt.task.run(['concurrent:log', 'log:done']);
  });
 
  grunt.initConfig({
    concurrent: {
        log: ['log:one', 'log:two']
    }
  });
};

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

  • 2014-01-25 - v0.1.0 - Initial release.
  • 2014-01-25 - v0.1.1 - Official release.
  • 2014-01-27 - v0.1.2 - Updated NPM keywords.
  • 2014-01-28 - v0.1.3 - Fix crash when presented with a Buffer in the stdout stream. Works even better now printing any arb stuff coming through the stream.
  • 2014-03-10 - v0.1.4 - Added option to keep console colors in the log output.
  • 2014-03-10 - v0.1.5 - Keep NPM version numbers happy.
  • 2015-02-20 - v0.1.6 - Hook stderr as well to log exceptions.
  • 2015-05-03 - v0.2.0 - Merged changes from Aliaksei Sapach and updated documentation on dealing with concurrent plugins.

Copyright (c) 2015 Werner van Deventer. Licensed under the MIT license.