2

I am using node-bunyan and bunyan-logstash-tcp in my nodejs application to send the logs to logstash (1.4.2) and elasticsearch (1.4.2).

Whenever the logstash server disconnects or is not reachable, my nodejs application crashes giving the following error

ERROR
-------
events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: connect ECONNREFUSED
    at errnoException (net.js:904:11)
    at Object.afterConnect [as oncomplete] (net.js:895:19)

bunyan-logstash-tcp should actually handle this error. Can anyone please help me to solve this nodejs crashing issue.

Val
  • 207,596
  • 13
  • 358
  • 360
DShah
  • 472
  • 5
  • 15
  • You should look into closed issues, one seems to be similar to yours: https://github.com/chris-rock/bunyan-logstash-tcp/issues/5 – Val May 22 '15 at 08:25
  • @Val I have already given max_connect_retries: -1 and retry_interval: 1000 – DShah May 22 '15 at 09:13

2 Answers2

4

I was able to figure out the issue. Error event needs to be handled while creating tcp bunyan stream

stream: bunyantcp.createStream({
        host: '127.0.0.1',
        port: 9998
    }).on('error', console.log)

This is not mentioned in the bunyan-logstash-tcp documentation, but was there in an example code.

UPDATE: Sample configuration

this.log = bunyan.createLogger({
name: name,
streams: [
    {
      level: 'error', 
      type: 'raw',
      serializers: bunyan.stdSerializers,
      stream: bunyantcp.createStream({
          application: process.title,
          max_connect_retries: 10, // Don't give up on reconnecting
          retry_interval: 1000 * 60     // Wait 1s between reconnect attempts
      }).on('error', console.log)
    }
  ],
  level: 'debug'
});
DShah
  • 472
  • 5
  • 15
  • Could you provide a more full example of your logging configuration? I have same issue but capturing the error isn't preventing the crash. – MrB Aug 03 '15 at 21:30
  • 1
    @MrB I have updated my answer with sample configuration. Hope this helps! – DShah Aug 04 '15 at 07:29
1

For logstash you could probably switch to UDP from TCP. There are few important advantages:

  • no connection issues (no reconnect problems, etc)
  • less performance overhead
  • less network overhead

Logentries' got a nice blogpost about it: https://blog.logentries.com/2014/07/tcp-or-udp-for-logging/

Take a look at some out-of-the box libs for logstash (elk stack) that support UDP: https://github.com/devmetrics/devmetrics-nodejs-core

Andrew Andrew
  • 302
  • 1
  • 2