Andrei Karpuszonak
Andrei Karpuszonak

Reputation: 9044

How to set 'short' output mode in bunyan by default

Regular (raw) logs in bunyan looks as following:

$ node myapp.js
{"name":"myapp","hostname":"myhost","pid":34572,"level":30,"msg":"start","time":"2013-01-04T07:47:25.814Z","v":0}
{"name":"myapp","hostname":"myhost","pid":34572,"widget_type":"wuzzle","level":30,"msg":"creating a wuzzle","time":"2013-01-04T07:47:25.815Z","v":0}

It is possible to use the "short" output mode, using the CLI, by piping logs to bunyan -o short

$ node myapp.js  | bunyan -o short
07:46:42.707Z  INFO myapp: start
07:46:42.709Z  INFO myapp: creating a wuzzle (widget_type=wuzzle)

Is it possible to use 'short' mode by default, so node myapp.js will produce short version of the logs?

Upvotes: 6

Views: 5166

Answers (2)

alex
alex

Reputation: 12275

You can pass a custom raw stream to bunyan and write your own pretty-printer:

  var Logger = require('bunyan')
    , Stream = require('stream')

  var stream = new Stream()
  stream.writable = true

  stream.write = function(obj) {
     // pretty-printing your message
     console.log(obj.msg)
  }

  var logger = new Logger({
     name: 'foo',
     streams: [{
        type: "raw",
        stream: stream,
     }],
     serializers: {
        err: Logger.stdSerializers.err,
        req: Logger.stdSerializers.req,
        res: Logger.stdSerializers.res,
     },
  })

  // -------------------------------

  logger.info('hello world')

You can look at this file that does exactly this job to pretty-print all messages.

Upvotes: 9

Evan Lucas
Evan Lucas

Reputation: 632

The bunyan cli tool actually parses the JSON that is output from running node myapp.js. To have it print pretty with the short option, you will still have to pipe it to bunyan. You can alias bunyan -o short by adding something to your ~/.profile or ~/.bash_profile depending on your system:

alias bunyans="bunyan -o short"

Then you could run it by doing the following:

$ node myapp.js | bunyans

Without piping it to bunyan (or some other tool that can parse/filter, reformat and display JSON), it will output JSON.

Upvotes: 0

Related Questions