Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 663
Andrei Karpushonak Avatar asked Jan 02 '14 09:01

Andrei Karpushonak


1 Answers

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.

like image 162
alex Avatar answered Oct 18 '22 11:10

alex