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

fix(logger.log): warn on invalid log-level #1062

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
11 changes: 8 additions & 3 deletions lib/logger.js
Expand Up @@ -7,6 +7,7 @@ const categories = require("./categories");
const configuration = require("./configuration");

const stackReg = /at (?:(.+)\s+\()?(?:(.+?):(\d+)(?::(\d+))?|([^)]+))\)?/;

function defaultParseCallStack(data, skipIdx = 4) {
const stacklines = data.stack.split("\n").slice(skipIdx);
const lineMatch = stackReg.exec(stacklines[0]);
Expand Down Expand Up @@ -68,7 +69,11 @@ class Logger {
}

log(level, ...args) {
const logLevel = levels.getLevel(level, levels.INFO);
let logLevel = levels.getLevel(level);
if (!logLevel) {
this._log(levels.WARN, 'log4js:logger.log: invalid value for log-level as first parameter given: ', level);
logLevel = levels.INFO;
}
if (this.isLevelEnabled(logLevel)) {
this._log(logLevel, args);
}
Expand Down Expand Up @@ -116,11 +121,11 @@ function addLevelMethods(target) {
);
const isLevelMethod = levelMethod[0].toUpperCase() + levelMethod.slice(1);

Logger.prototype[`is${isLevelMethod}Enabled`] = function() {
Logger.prototype[`is${isLevelMethod}Enabled`] = function () {
return this.isLevelEnabled(level);
};

Logger.prototype[levelMethod] = function(...args) {
Logger.prototype[levelMethod] = function (...args) {
this.log(level, ...args);
};
}
Expand Down
4 changes: 3 additions & 1 deletion test/tap/newLevel-test.js
Expand Up @@ -237,8 +237,10 @@ test("../../lib/logger", batch => {
logger.log(log4js.levels.getLevel("LEVEL_DOES_NEXT_EXIST"), "Event 2");

const events = recording.replay();
t.equal(events[0].level.toString(), "INFO", "should fall back to INFO");
t.equal(events[0].level.toString(), "WARN", "should log warning");
t.equal(events[1].level.toString(), "INFO", "should fall back to INFO");
t.equal(events[2].level.toString(), "WARN", "should log warning");
t.equal(events[3].level.toString(), "INFO", "should fall back to INFO");
t.end();
});

Expand Down