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

feat: adds helper function for highest log level #2514

Open
wants to merge 1 commit 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
22 changes: 22 additions & 0 deletions lib/winston/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,28 @@ class Logger extends Transform {
}
}

/* eslint-disable valid-jsdoc */
/**
* Helper method to get the highest logging level associated with a logger
*
* @returns { number | null } - The highest configured logging level, null
* for invalid configuration
*/
getHighestLogLevel() {
// This can be null, if this.level has an invalid value
const configuredLevelValue = getLevelValue(this.levels, this.level);

// If there are no transports, return the level configured at the logger level
if (!this.transports || this.transports.length === 0) {
return configuredLevelValue;
}

return this.transports.reduce((max, transport) => {
const levelValue = getLevelValue(this.levels, transport.level);
return levelValue !== null && levelValue > max ? levelValue : max;
}, configuredLevelValue);
}

isLevelEnabled(level) {
const givenLevelValue = getLevelValue(this.levels, level);
if (givenLevelValue === null) {
Expand Down
18 changes: 18 additions & 0 deletions test/unit/winston/logger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,9 @@ describe('Logger Instance', function () {
transports: [new winston.transports.Console()]
});

assume(logger.getHighestLogLevel).is.a('function');
assume(logger.getHighestLogLevel()).equals(4);

assume(logger.isLevelEnabled).is.a('function');

assume(logger.isErrorEnabled).is.a('function');
Expand Down Expand Up @@ -380,6 +383,9 @@ describe('Logger Instance', function () {
transports: [transport]
});

assume(logger.getHighestLogLevel).is.a('function');
assume(logger.getHighestLogLevel()).equals(5);

assume(logger.isLevelEnabled).is.a('function');

assume(logger.isErrorEnabled).is.a('function');
Expand Down Expand Up @@ -411,6 +417,9 @@ describe('Logger Instance', function () {
transports: []
});

assume(logger.getHighestLogLevel).is.a('function');
assume(logger.getHighestLogLevel()).equals(4);

assume(logger.isLevelEnabled).is.a('function');

assume(logger.isErrorEnabled).is.a('function');
Expand Down Expand Up @@ -446,6 +455,9 @@ describe('Logger Instance', function () {
transports: [new winston.transports.Console()]
});

assume(logger.getHighestLogLevel).is.a('function');
assume(logger.getHighestLogLevel()).equals(1);

assume(logger.isLevelEnabled).is.a('function');

assume(logger.isBadEnabled).is.a('function');
Expand All @@ -472,6 +484,9 @@ describe('Logger Instance', function () {
transports: []
});

assume(logger.getHighestLogLevel).is.a('function');
assume(logger.getHighestLogLevel()).equals(1);

assume(logger.isLevelEnabled).is.a('function');

assume(logger.isBadEnabled).is.a('function');
Expand Down Expand Up @@ -501,6 +516,9 @@ describe('Logger Instance', function () {
transports: [transport]
});

assume(logger.getHighestLogLevel).is.a('function');
assume(logger.getHighestLogLevel()).equals(2);

assume(logger.isLevelEnabled).is.a('function');

assume(logger.isBadEnabled).is.a('function');
Expand Down