99

I want to log to stdout (the config environment) when a gulp task is running or has run.

Something like this:

gulp.task('scripts', function () {
  var enviroment = argv.env || 'development';
  var config = gulp.src('config/' + enviroment + '.json')
      .pipe(ngConstant({name: 'app.config'}));
  var scripts = gulp.src('js/*');

  return es.merge(config, scripts)
    .pipe(concat('app.js'))
    .pipe(gulp.dest('app/dist'))
    .on('success', function() { 
      console.log('Configured environment: ' + environment);
    });
});

I am not sure what event I should be responding to or where to find a list of these. Any pointers? Many thanks.

Rimian
  • 36,864
  • 16
  • 117
  • 117

3 Answers3

187

(In December 2017, the gulp-util module, which provided logging, was deprecated. The Gulp team recommended that developers replace this functionality with the fancy-log module. This answer has been updated to reflect that.)

fancy-log provides logging and was originally built by the Gulp team.

var log = require('fancy-log');
log('Hello world!');

To add logging, Gulp's API documentation tell us that .src returns:

Returns a stream of Vinyl files that can be piped to plugins.

Node.js's Stream documentation provides a list of events. Put together, here's an example:

gulp.task('default', function() {
    return gulp.src('main.scss')
        .pipe(sass({ style: 'expanded' }))
        .on('end', function(){ log('Almost there...'); })
        .pipe(minifycss())
        .pipe(gulp.dest('.'))
        .on('end', function(){ log('Done!'); });
});

Note: The end event may be called before the plugin is complete (and has sent all of its own output), because the event is called when "all data has been flushed to the underlying system".

Jacob Budin
  • 9,753
  • 4
  • 32
  • 35
18

(PLEASE NOTE - In December 2017, the gulp-util module was deprecated.)

To build on the answer by Jacob Budin, I recently tried this and found it useful.

var gulp = require("gulp");
var util = require("gulp-util");
var changed = require("gulp-changed");

gulp.task("copyIfChanged", function() {
    var nSrc=0, nDes=0, dest="build/js";
    gulp.src("app/**/*.js")
    .on("data", function() { nSrc+=1;})
    .pipe(changed(dest)) //filter out src files not newer than dest
    .pipe(gulp.dest(dest))
    .on("data", function() { nDes+=1;})
    .on("finish", function() {
        util.log("Results for app/**/*.js");
        util.log("# src files: ", nSrc);
        util.log("# dest files:", nDes);
    });
}

Update (April 2021): Re the deprecated module issue. Try replacing util.log from require('gulp-util') with log from require('fancy-log'). The rest of the approach should still work. Disclaimer: I haven't tested this.

Trevedhek
  • 4,138
  • 2
  • 20
  • 16
  • `.on("end")` and `.on("finish")` differ by what? – Frank N Dec 05 '16 at 14:34
  • 3
    As I understand it, `writable.on('finish')` will fire when `readable.on('end')` may not. The exact event may not matter in this case - I was more focused on logging useful stats. – Trevedhek Dec 06 '16 at 17:50
  • [As per the current (7.5.0) documentation:](https://nodejs.org/api/stream.html#stream_events_finish_and_end) "The `'finish'` and `'end'` events are from the `stream.Writable` and `stream.Readable` classes, respectively." @FrankNocke – msanford Feb 02 '17 at 15:43
  • 1
    `gulp-util` has been deprecated, use `fancy-log` instead – slajma May 08 '20 at 04:44
5

Sadly gulp.util was deprecated. Use fancy-log instead: https://www.npmjs.com/package/fancy-log.

hong4rc
  • 3,999
  • 4
  • 21
  • 40
bsesic
  • 589
  • 5
  • 7