Simple text file logging for Grunt and task output.
npm install logfile-grunt~0.4.0
shell
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:
`js
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.
$3
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.
#### options.filePath
Type: String
Default value: './logs/grunt.log'
A file name (and path) where your log file will be created.
#### options.clearLogFile
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.
#### options.keepColors
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.
#### options.textEncoding
Type: String
Default value: utf-8Specify the encoding to use when writing to the text file.
#### options.excludePattern
Type:
RegExp
Default value: nullProvide a regular expression pattern to filter out certain output from being logged to file (Issue #9).
$3
#### Default Options
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.
`js
require('logfile-grunt')(grunt);
`
#### Custom File Option
In this example, you can provide a custom file path for your log file by providing a name and path in the options object.
`js
require('logfile-grunt')(grunt, { filePath: './logs/MyCustomLogs.txt' });
`
#### Clear Log Option
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.
`js
require('logfile-grunt')(grunt, { filePath: './logs/ClearedOnEveryRun.log', clearLogFile: true });
`
#### Keep Colors Option
In this example, the log file will retain the console color codes used by Grunt.
`js
require('logfile-grunt')(grunt, { keepColors: true });
`
#### Task Specific Logs
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.
`js
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']);
`
#### Using Concurrent and Parallelize
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.
`js
// Require logfile-grunt before loading other Grunt 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 started.
logfile(grunt);
grunt.task.run(['concurrent:log', 'log:done']);
});
grunt.initConfig({
concurrent: {
log: ['log:one', 'log:two']
}
});
};
``