Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event 'finish' is fired before actual flushing the buffer #90

Open
servernoj opened this issue Jul 15, 2019 · 3 comments
Open

Event 'finish' is fired before actual flushing the buffer #90

servernoj opened this issue Jul 15, 2019 · 3 comments

Comments

@servernoj
Copy link

servernoj commented Jul 15, 2019

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()
}
@ttrevorr
Copy link

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

@vanthome
Copy link
Owner

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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants