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

When Winston logger is fixed, remove hacky workaround that detects file stream flush / buffer write completion #272

Open
danielweck opened this issue Aug 19, 2019 · 2 comments

Comments

@danielweck
Copy link
Member

master branch:

/* eslint-disable no-underscore-dangle */
// Properly wait that loggers write to file before exitting
// See https://github.com/winstonjs/winston/issues/228
winston.logAndExit = function logAndExit(level, msg, codeOrCallback) {
const self = this;
this.log(level, msg, () => {
let numFlushes = 0;
let numFlushed = 0;
Object.keys(self.default.transports).forEach((k) => {
if (self.default.transports[k]._stream) {
numFlushes += 1;
self.default.transports[k]._stream.once('finish', () => {
numFlushed += 1;
if (numFlushes === numFlushed) {
if (typeof codeOrCallback === 'function') {
codeOrCallback();
} else {
process.exit(codeOrCallback);
}
}
});
self.default.transports[k]._stream.end();
}
});
if (numFlushes === 0) {
if (typeof codeOrCallback === 'function') {
codeOrCallback();
} else {
process.exit(codeOrCallback);
}
}
});
};

Ace-GUI branch (updated Winston NPM dependency, still the same problem):

// Properly wait that loggers write to file before exitting
// See https://github.com/winstonjs/winston/issues/228
winston.logAndWaitFinish = async (level, msg) => {
return new Promise(async (resolve, reject) => {
winston.log(level, msg);
for (const transport of transports) {
try {
await closeTransportAndWaitForFinish(transport);
} catch (err) {
console.log(err);
}
}
resolve();
});
};

const closeTransportAndWaitForFinish = async (transport) => {
if (!transport.close) {
return Promise.resolve();
}
return new Promise((resolve, reject) => {
transport._doneFinish = false;
function done() {
if (transport._doneFinish) {
return;
}
transport._doneFinish = true;
resolve();
}
setTimeout(() => {
done();
}, 5000);
const finished = () => {
done();
};
if (transport._stream) {
transport._stream.once('finish', finished);
transport._stream.end();
} else {
transport.once('finish', finished);
transport.close();
}
});
}

@danielweck
Copy link
Member Author

Some context:

My report on the Winston issue tracker:
winstonjs/winston#228 (comment)

Somebody recently proposed a fix (submitted a PR):
winstonjs/winston-transport#50

My related comment about the current workaround:
winstonjs/winston-transport#49 (comment)

@danielweck
Copy link
Member Author

Related Ace issue about concurrent access to log files:
#68 (comment)

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

1 participant