winston-elasticsearch
winston-elasticsearch copied to clipboard
Event 'finish' is fired before actual flushing the buffer
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()
}
I am seeing the exact same issue... any updates?
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:
- You wait for twice the
flushInterval
before youprocess.exit(10)
- You set
buffering
tofalse
I have tested none of them, they might work, please try out.
Can you please check this again with the latest version and let me know?