node-bunyan icon indicating copy to clipboard operation
node-bunyan copied to clipboard

Default to pretty-printed output if process.stdout.isTTY

Open isaacs opened this issue 12 years ago • 29 comments

It's pretty common in development to do a lot of node server.js and watch the output. Piping that to bunyan is a bit annoying.

It should default to doing the pretty stuff if process.stdout.isTTY === true.

isaacs avatar May 02 '12 03:05 isaacs

How about attaching a formatter to a Logger stream? Or the (sub)Logger in general.

var log = new Logger({
  name: "amon",
  streams: [
    {
      level: "info",
      stream: process.stdout,
      formatter: "pretty"
    },
    {
      level: "error",
      stream: "./error.log" // Uses default JSON formatter
    }
  ]
}

mattijs avatar May 09 '12 08:05 mattijs

Getting closer. type == 'raw' streams are going in soon (see issue #8).

trentm avatar Jun 21 '12 21:06 trentm

Anything new here? This is the only feature I'm missing to fully use bunyan.

dignifiedquire avatar Nov 22 '12 12:11 dignifiedquire

I have some work started on the 1.x branch, but it is a ways from being ready. I also have a looming deadline mid-December at work, so I probably won't get back to this until after then.

trentm avatar Nov 22 '12 17:11 trentm

:+1: Yes, please!

mcandre avatar May 01 '13 14:05 mcandre

+1. I've created a minimal writable stream that prints out the level/message and am using that for the time being. (Maybe there is a better workaround?)

wachunga avatar Jul 23 '13 21:07 wachunga

Any updates on this?

I've been using a simple shell script to save those keystrokes:

#!/usr/bin/env sh
node . | bunyan -o short

But I had some free time so I tried writing a stream to pipe the output through bunyan but ended up with a catch-22 situation, the spawned process only exits when the main process does and the main process only exits once the child does.

It should work if your main process is long running (web servers, etc) but isn't ideal for short running apps like cli tools.

var spawn   = require('child_process').spawn;
var through = require('through');
var path    = require('path');
var fs      = require('fs');

var prettyStream = function(args) {
  args = args || ['-o', 'short'];
  var bin = path.resolve(path.dirname(require.resolve('bunyan')), '..', 'bin', 'bunyan');
  var stream = through(function write(data) {
    this.queue(data);
  }, function end () {
    this.queue(null);
  });

  if(bin && fs.existsSync(bin)) {
    var formatter = spawn(bin, ['-o', 'short'], {
      stdio: [null, process.stdout, process.stderr]
    });
    stream.pipe(formatter.stdin);
  }

  return stream;
};

bunyan.createLogger({
  name: 'test',
  stream: process.stdout.isTTY ? prettyStream() : process.stdout,
  level: 'info'
});

jameswyse avatar Aug 10 '13 12:08 jameswyse

@jameswyse unfortunately I haven't had a chance yet :(

I have a big release of work stuff in a couple weeks. I hope to get a chance for this ticket (among a few others) after that.

trentm avatar Aug 12 '13 03:08 trentm

Whatever happened to this? Bunyan would be perfect if this were implemented. Needing to pipe to bunyan is quite annoying. I would switch everything to bunyan if it were not for this one issue.

bzuillsmith avatar Aug 08 '14 05:08 bzuillsmith

+1

stevenzyang avatar Sep 10 '14 23:09 stevenzyang

We have some command line utils which fetch JSON logs from the server, and I keep forgetting to pipe the result to bunyan. I wish something like this existed so we could (within the CLI client) check if output it to TTY and if so call bunyan.formatOutput(...) instead of just printing out the raw JSON.

mkopinsky avatar Dec 04 '14 17:12 mkopinsky

+1

ross-nordstrom avatar May 09 '15 12:05 ross-nordstrom

+1

adalinesimonian avatar Aug 21 '15 03:08 adalinesimonian

+1 looking forward for this feature.

josnidhin avatar Sep 03 '15 07:09 josnidhin

+1

d7h avatar Sep 15 '15 11:09 d7h

This is the main thing that stops me from choosing bunyan as a logger lib for my project, so +1!

th0r avatar Sep 18 '15 08:09 th0r

@trentm Are you looking at this already or you still don't have time?

th0r avatar Sep 18 '15 08:09 th0r

+1

slavafomin avatar Apr 26 '16 14:04 slavafomin

I neeeeeeed this! +1

basickarl avatar May 26 '16 08:05 basickarl

After trying out this logger package, this is one of the first features I found myself wanting and is the only one that is missing at the moment. Any news on where this might be at?

ShaggyDev avatar Jun 21 '16 00:06 ShaggyDev

+1 any updates?

npgauth avatar Jun 30 '16 14:06 npgauth

Nothing after 4 years?

vizo avatar Nov 22 '16 05:11 vizo

To anyone still looking, this works well: https://github.com/mrrama/node-bunyan-prettystream

import bunyan from 'bunyan';
import PrettyStream from 'bunyan-prettystream';

const stream = new PrettyStream();
stream.pipe(process.stdout);

const logger = bunyan.createLogger({
  name: 'npm-search',
  streams: [{
    stream,
  }],
});

vvo avatar Dec 18 '16 22:12 vvo

Or even this: https://github.com/benbria/bunyan-debug-stream

seems to work better

vvo avatar Dec 18 '16 23:12 vvo

  • 1

PetrochukM avatar Jan 15 '17 16:01 PetrochukM

Now that bunyan-cli doesn't process ctrl+c/SIGINT anymore (in patch release!) this is even more important.

thepatrick avatar Jun 30 '17 06:06 thepatrick

@trentm it's been a year since your last reference of this issue. More than 5 years since the issue was first opened. Would a PR be accepted? It seems this one is related: https://github.com/trentm/node-bunyan/pull/102

ubershmekel avatar Sep 25 '17 23:09 ubershmekel

I just send this to a nodejs stream and manually console.log (assuming you have not overwritten console.log behavior)

let _stream = new stream.Writable()

let _log = bunyan.createLogger({
	name: this._process_name,
	level: this._log_level,
	serializers: {
		req: bunyan.stdSerializers.req
	},
	env: this._env
})

_stream._write = (chunk, encoding, next) => {
	console.log(JSON.parse(chunk))
	next()
}


_log.addStream({
	name: 'some new stream',
	stream: self._stream,
	level: self._log_level,
	serializers: {
		req: bunyan.stdSerializers.req
	},
	env: this._env
})

jeffbeagley avatar Apr 05 '19 17:04 jeffbeagley

Are there any updates to this? Would also like to use this in our CLI tool!

sidpremkumar avatar Jan 12 '23 02:01 sidpremkumar