Pino: Using pretty print in Gulp

Created on 9 Mar 2017  路  3Comments  路  Source: pinojs/pino

I'm using pino for logging on the backend, and I'm quite happy with it and its pretty printer, but if used within gulp (using spawn to run it) the coloured output doesn't work.

When I deploy my project, logging is pretty printed through the setting in package.json (currently:{ ..., "scripts": { "start": "node app.js | pino", ... })) and everything works fine.

During development I rely on gulp, and I managed to pipe the output to the pretty print with this portion of code (adapted from: nodemon/bunyan integration):

var   gulp = require('gulp'),
      nodemon = require('gulp-nodemon'),
      spawn   = require('child_process').spawn,
       ...

gulp.task('nodemon', function (cb) {

        var pino;

    nodemon({ ..., stdout: false, readable: false })

      .on('readable', function() {

            pino && pino.kill();

            pino = spawn('pino', [ '-l' ]);

            pino.stdout.pipe(process.stdout);
            pino.stderr.pipe(process.stderr);

            this.stdout.pipe(pino.stdin);
            this.stderr.pipe(pino.stdin);

      });

});

But although the output is nicely formatted, it is not coloured. I searched online, and most probably the issue is that the spawned pino process doesn't support colouring (I tried few solutions but none of them worked).

Since I'm just starting with nodejs, I wonder if there is an easier way to achieve it, like requiring the pino module within gulp and then piping it straight away.

Have you got any suggestions or possible solutions?

Most helpful comment

@jsumners During development, I use gulp for two reasons:

  • any change in the UI code is pushed through browserSync
  • modification on the backend code trigger an automatic restart of the server

That's my first node project, but to my understanding using { "scripts" : ... } I'll have to kill and restart the server manually, so I don't think it fits my case! FIX, sorry, got your point, but I have a minifier and a sass compiler, therefore I need gulp for the UI!

@davidmarkclements I took your code and modified to fit my case (spawn was the cause of losing the colours), that's the new snippet:

var   gulp = require('gulp'),
      nodemon = require('gulp-nodemon'),
      pretty = require('pino').pretty,
       ...

gulp.task('nodemon', function (cb) {

        var pino;

    nodemon({ ..., stdout: false, readable: false })

      .on('readable', function() {

            this.stdout.pipe(pretty()).pipe(process.stdout)
            this.stderr.pipe(pretty()).pipe(process.stdout)

      });

});

And now everything works fine, thanks! :)

All 3 comments

Why not use use npm scripts? For example:

{
  "scripts": {
    "nodemon": "nodemon your-app.js"
  }
}

like @jsumners suggests the command line is usually the most powerful and terse way to deal with these sorts of things

for completeness though - if you want to do this in the group file just require pino and use the pretty method to create a transform stream

var pretty = require('pino').pretty

then later in your readable callback

     pino.stdout.pipe(pretty()).pipe(process.stdout)

closing now as I think that we've answered in the two normative ways to handle this

@jsumners During development, I use gulp for two reasons:

  • any change in the UI code is pushed through browserSync
  • modification on the backend code trigger an automatic restart of the server

That's my first node project, but to my understanding using { "scripts" : ... } I'll have to kill and restart the server manually, so I don't think it fits my case! FIX, sorry, got your point, but I have a minifier and a sass compiler, therefore I need gulp for the UI!

@davidmarkclements I took your code and modified to fit my case (spawn was the cause of losing the colours), that's the new snippet:

var   gulp = require('gulp'),
      nodemon = require('gulp-nodemon'),
      pretty = require('pino').pretty,
       ...

gulp.task('nodemon', function (cb) {

        var pino;

    nodemon({ ..., stdout: false, readable: false })

      .on('readable', function() {

            this.stdout.pipe(pretty()).pipe(process.stdout)
            this.stderr.pipe(pretty()).pipe(process.stdout)

      });

});

And now everything works fine, thanks! :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mcollina picture mcollina  路  3Comments

davidmarkclements picture davidmarkclements  路  6Comments

glensc picture glensc  路  3Comments

grbr picture grbr  路  6Comments

NicolaiSchmid picture NicolaiSchmid  路  4Comments