Skip to content

Commit

Permalink
feat: adds helper function for highest log level (#2514)
Browse files Browse the repository at this point in the history
- Adds helper function to get the highest log level associated with
  an helper.

ref: #837
  • Loading branch information
tapaj authored Nov 11, 2024
1 parent 1b9cef8 commit c69cdb0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
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

0 comments on commit c69cdb0

Please sign in to comment.