diff --git a/lib/winston/container.js b/lib/winston/container.js index 57720306a..4a53f68fd 100644 --- a/lib/winston/container.js +++ b/lib/winston/container.js @@ -65,6 +65,19 @@ module.exports = class Container { return this.add(id, options); } + /** + * Retreives all `winston.Logger` instances for the specified `regex`. + * @param {!string} regex - The regex for filtering the Loggers to get. + * @returns {Map} - A Map of Logger instances matching the regex. + */ + getAll(regex) { + if (regex) { + return new Map([...this.loggers].filter(([key]) => key.match(regex))); + } + + return this.loggers; + } + /** * Check if the container has a logger with the id. * @param {?string} id - The id of the Logger instance to find. diff --git a/test/unit/winston/container.test.js b/test/unit/winston/container.test.js index 3008ee08b..bbb0aa17c 100644 --- a/test/unit/winston/container.test.js +++ b/test/unit/winston/container.test.js @@ -23,6 +23,29 @@ describe('Container', function () { assume(container.get('default-test')).equals(defaultTest); }); + it('.getAll(default-test)', function () { + container.add('appLogger'); + container.add('serviceLogger'); + + var allLogger = container.getAll(); + var appLogger = container.getAll(/^app/); + var matchLogg = container.getAll(/Logger$/); + var emptyLogg = container.getAll(/Library/); + + assume(allLogger.size).equals(3); + assume(allLogger.has('appLogger')).true(); + assume(allLogger.has('serviceLogger')).true(); + + assume(appLogger.size).equals(1); + assume(appLogger.has('appLogger')).true(); + + assume(matchLogg.size).equals(2); + assume(matchLogg.has('appLogger')).true(); + assume(matchLogg.has('serviceLogger')).true(); + + assume(emptyLogg.size).equals(0); + }); + it('.has(default-test)', function () { assume(container.has('default-test')).true(); });