winston-elasticsearch icon indicating copy to clipboard operation
winston-elasticsearch copied to clipboard

Event 'finish' is fired before actual flushing the buffer

Open servernoj opened this issue 5 years ago • 3 comments

I am handling uncaughtException event in NodeJS application to assure that logs are flushed to ES (in my case it is AWS ES domain).

Here is the code snippet of the handler:

logger.on('finish', () => process.exit(10)).end()

where logger is created by calling winston.createLogger({...}) sometime earlier.

It appears that calling process.exit(10) on finish event emitted by the logger terminates the process of writing logs out so they are not visible on the ES side.

Having above code snippet replaced with

logger.end()

solves the problem but the application reports exit code of 0 (i.e. everything is OK) instead of desired value of 10 (i.e. something went wrong). I want to use exit code for automation.

I also tried calling end() directly at ElasticsearchTransport instead of calling it off of logger instance:

const est = logger.transports.find(e => e instanceof ElasticsearchTransport)
if (est) {
   est.end()
}

servernoj avatar Jul 15 '19 17:07 servernoj

I am seeing the exact same issue... any updates?

ttrevorr avatar Jul 25 '19 15:07 ttrevorr

Ok, I understand this issue. This is probably because we emit the even logged immediately and not only when the message has been written by the bulk writer. It is however pretty hard to keep track of this if bulk writing (batching) is used. As an immediate fix, I suggest two ways:

  1. You wait for twice the flushInterval before you process.exit(10)
  2. You set buffering to false

I have tested none of them, they might work, please try out.

vanthome avatar Aug 28 '19 21:08 vanthome

Can you please check this again with the latest version and let me know?

vanthome avatar Jun 14 '20 09:06 vanthome