How to update streams in order to substitute a stream with a new one?

397 views
Skip to first unread message

Giovanni Gaglione

unread,
Dec 30, 2014, 10:02:01 PM12/30/14
to bunyan-...@googlegroups.com
Does bunyan offer an API to update streams at runtime?

My case follows:
I have a bunyan logging module, that sends logs to a machine on the network. A net.connect() stream is used.
Let's assume the the endpoint on the network fails, so bunyan stream disconnects from it.
Now I am handling the fail reconnecting again to the endpoint on the network, but the doc does not provide any method to update dynamically streams.   

Trent Mick

unread,
Jan 2, 2015, 7:38:36 PM1/2/15
to Giovanni Gaglione, bunyan-...@googlegroups.com
Hi Giovanni,

Two things.

1. I don't know your code, but I'd consider having your stream deal with reconnection logic (e.g. using node-backoff or similar).

2. There is the `<log>.addStream` method that you can use to *add* a stream to a Logger instance. There is no formal API right now to replace or remove streams from a logger. However, currently at least, `log.streams` is just an array, so if you know the index of the stream to remove, then you can do so like this:

```
> var bunyan = require('bunyan')
undefined
> var log = bunyan.createLogger({name: 'play'})
undefined
> log.streams
[ { type: 'stream',
    stream:
     { _connecting: false,
       _handle: [Object],
       _readableState: [Object],
       readable: false,
       domain: null,
       _events: [Object],
       _maxListeners: 10,
       _writableState: [Object],
       writable: true,
       allowHalfOpen: false,
       onend: null,
       destroyed: false,
       bytesRead: 0,
       _bytesDispatched: 240,
       _pendingData: null,
       _pendingEncoding: '',
       columns: 136,
       rows: 51,
       _type: 'tty',
       fd: 1,
       _isStdio: true,
       destroySoon: [Function],
       destroy: [Function] },
    closeOnExit: false,
    level: 30,
    raw: false } ]
> log.addStream({type: 'file', path: '/var/tmp/play.log'});
undefined
> log.streams
[ { type: 'stream',
    stream:
     { _connecting: false,
       _handle: [Object],
       _readableState: [Object],
       readable: false,
       domain: null,
       _events: [Object],
       _maxListeners: 10,
       _writableState: [Object],
       writable: true,
       allowHalfOpen: false,
       onend: null,
       destroyed: false,
       bytesRead: 0,
       _bytesDispatched: 1784,
       _pendingData: null,
       _pendingEncoding: '',
       columns: 136,
       rows: 51,
       _type: 'tty',
       fd: 1,
       _isStdio: true,
       destroySoon: [Function],
       destroy: [Function] },
    closeOnExit: false,
    level: 30,
    raw: false },
  { type: 'file',
    path: '/var/tmp/play.log',
    raw: false,
    level: 30,
    stream:
     { _writableState: [Object],
       writable: true,
       domain: null,
       _events: [Object],
       _maxListeners: 10,
       path: '/var/tmp/play.log',
       fd: 11,
       flags: 'a',
       mode: 438,
       start: undefined,
       pos: undefined,
       bytesWritten: 0 },
    closeOnExit: true } ]
> log.streams = log.streams.slice(1)
[ { type: 'file',
    path: '/var/tmp/play.log',
    raw: false,
    level: 30,
    stream:
     { _writableState: [Object],
       writable: true,
       domain: null,
       _events: [Object],
       _maxListeners: 10,
       path: '/var/tmp/play.log',
       fd: 11,
       flags: 'a',
       mode: 438,
       start: undefined,
       pos: undefined,
       bytesWritten: 0 },
    closeOnExit: true } ]
```


Cheers,
Trent

--
Trent Mick
Reply all
Reply to author
Forward
0 new messages