Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
isaacs opened this issue May 2, 2012 · 29 comments
Open

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

isaacs opened this issue May 2, 2012 · 29 comments

Comments

@isaacs
Copy link
Contributor

isaacs commented May 2, 2012

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.

@mattijs
Copy link

mattijs commented May 9, 2012

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
    }
  ]
}

@trentm
Copy link
Owner

trentm commented Jun 21, 2012

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

@dignifiedquire
Copy link

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

@trentm
Copy link
Owner

trentm commented Nov 22, 2012

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.

@mcandre
Copy link

mcandre commented May 1, 2013

👍 Yes, please!

@wachunga
Copy link

+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?)

@jameswyse
Copy link

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'
});

@trentm
Copy link
Owner

trentm commented Aug 12, 2013

@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.

@bzuillsmith
Copy link

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.

@stevenzyang
Copy link

+1

@mkopinsky
Copy link

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.

@trentm trentm added this to the 2.0 milestone Jan 18, 2015
@ross-nordstrom
Copy link

+1

@adalinesimonian
Copy link

+1

@josnidhin
Copy link

+1 looking forward for this feature.

@d7h
Copy link

d7h commented Sep 15, 2015

+1

@th0r
Copy link

th0r commented Sep 18, 2015

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

@th0r
Copy link

th0r commented Sep 18, 2015

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

@slavafomin
Copy link

+1

@basickarl
Copy link

I neeeeeeed this! +1

@ShaggyDev
Copy link

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?

@npgauth
Copy link

npgauth commented Jun 30, 2016

+1 any updates?

@vizo
Copy link

vizo commented Nov 22, 2016

Nothing after 4 years?

@vvo
Copy link

vvo commented Dec 18, 2016

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
Copy link

vvo commented Dec 18, 2016

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

seems to work better

@PetrochukM
Copy link

  • 1

@thepatrick
Copy link

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

@ubershmekel
Copy link

@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: #102

@jeffbeagley
Copy link

jeffbeagley commented Apr 5, 2019

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
})

@sidpremkumar
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests