diff --git a/lib/logger.js b/lib/logger.js index 7574090..7a77583 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -384,19 +384,35 @@ const flushAll = function(cb) { if (!cb || typeof cb !== 'function') { cb = debug; } + + // Variables used to wait for all the flushes to be completed + let counter = 0; + const loggersCount = loggers.length; + const complete = (errors) => { + if (errors.length > 0) { + return cb(`The following errors happened while flushing all loggers: ${errors}`); + } + return cb(); + + }; + const errors = []; loggers.forEach((logger) => { logger._flush((err) => { if (err) { errors.push(err); } + // Counter is incremented after each flush + if (++counter === loggersCount) { + + /* + * Flushed all the loggers + * Completes by calling the callback + */ + complete(errors); + } }); }); - - if (errors.length > 0) { - return cb(`The following errors happened while flushing all loggers: ${errors}`); - } - return cb(); }; exports.Logger = Logger; diff --git a/test/logger.js b/test/logger.js index 1936866..479b958 100644 --- a/test/logger.js +++ b/test/logger.js @@ -288,9 +288,6 @@ describe('Index meta', function() { }); describe('Input validation', function() { - afterEach(function(done) { - Logger.flushAll(done); - }); it('Sanity checks for Ingestion Key', function(done) { const bogusKeys = [ 'THIS KEY IS TOO LONG THIS KEY IS TOO LONG THIS KEY IS TOO LONG THIS KEY IS TOO LONG THIS KEY IS TOO LONG THIS KEY IS TOO LONG'