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

Addition of Logger.getAll() method as discussed in #970 #2515

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/winston/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<Logger>} - 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.
Expand Down
23 changes: 23 additions & 0 deletions test/unit/winston/container.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
Loading