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

refactor: clearer logic for invalid level and LOG synonym #1264

Merged
merged 1 commit into from May 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 8 additions & 10 deletions lib/logger.js
Expand Up @@ -80,19 +80,17 @@ class Logger {
}

log(level, ...args) {
let logLevel = levels.getLevel(level);
const logLevel = levels.getLevel(level);
if (!logLevel) {
// allow LOG to be synonym of INFO
if ((level && level.trim().indexOf(" ") !== -1) || args.length === 0) {
args = [level, ...args];
if (configuration.validIdentifier(level) && args.length > 0) {
// logLevel not found but of valid signature, WARN before fallback to INFO
this.log(levels.WARN, 'log4js:logger.log: valid log-level not found as first parameter given:', level);
this.log(levels.INFO, `[${level}]`, ...args);
} else {
this._log(levels.WARN, ['log4js:logger.log: invalid value for log-level as first parameter given:', level]);
args = [`[${level}]`, ...args];
// apart from fallback, allow .log(...args) to be synonym with .log("INFO", ...args)
this.log(levels.INFO, level, ...args);
}
// fallback to INFO
logLevel = levels.INFO;
}
if (this.isLevelEnabled(logLevel)) {
} else if (this.isLevelEnabled(logLevel)) {
this._log(logLevel, args);
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/tap/newLevel-test.js
Expand Up @@ -243,14 +243,14 @@ test("../../lib/logger", batch => {
const events = recording.replay();

t.equal(events[0].level.toString(), "WARN", "should log warning");
t.equal(events[0].data[0], "log4js:logger.log: invalid value for log-level as first parameter given:");
t.equal(events[0].data[0], "log4js:logger.log: valid log-level not found as first parameter given:");
t.equal(events[0].data[1], "LEVEL_DOES_NOT_EXIST");
t.equal(events[1].level.toString(), "INFO", "should fall back to INFO");
t.equal(events[1].data[0], "[LEVEL_DOES_NOT_EXIST]");
t.equal(events[1].data[1], "Event 1");

t.equal(events[2].level.toString(), "WARN", "should log warning");
t.equal(events[2].data[0], "log4js:logger.log: invalid value for log-level as first parameter given:");
t.equal(events[2].data[0], "log4js:logger.log: valid log-level not found as first parameter given:");
t.equal(events[2].data[1], undefined);
t.equal(events[3].level.toString(), "INFO", "should fall back to INFO");
t.equal(events[3].data[0], "[undefined]");
Expand Down