trentm / node-bunyan

a simple and fast JSON logging module for node.js services
Other
7.18k stars 517 forks source link

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

Open isaacs opened 12 years ago

isaacs commented 12 years ago

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 commented 12 years ago

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 commented 12 years ago

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

dignifiedquire commented 11 years ago

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

trentm commented 11 years ago

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 commented 11 years ago

:+1: Yes, please!

wachunga commented 11 years ago

+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 commented 11 years ago

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 commented 11 years ago

@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 commented 10 years ago

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 commented 10 years ago

+1

mkopinsky commented 9 years ago

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.

ross-nordstrom commented 9 years ago

+1

adalinesimonian commented 9 years ago

+1

josnidhin commented 9 years ago

+1 looking forward for this feature.

d7h commented 9 years ago

+1

th0r commented 9 years ago

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

th0r commented 9 years ago

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

slavafomin commented 8 years ago

+1

basickarl commented 8 years ago

I neeeeeeed this! +1

ShaggyDev commented 8 years ago

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 commented 8 years ago

+1 any updates?

vizo commented 7 years ago

Nothing after 4 years?

vvo commented 7 years ago

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 commented 7 years ago

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

seems to work better

PetrochukM commented 7 years ago
thepatrick commented 7 years ago

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

ubershmekel commented 7 years ago

@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

jeffbeagley commented 5 years ago

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 commented 1 year ago

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