mirror of
https://github.com/log4js-node/log4js-node.git
synced 2025-12-08 19:26:01 +00:00
refactor: level.js
pick the getLevel function back from log4js.js
This commit is contained in:
parent
a0bab5b3af
commit
bdeb7b9c41
103
lib/levels.js
103
lib/levels.js
@ -1,66 +1,85 @@
|
||||
"use strict";
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* @name Level
|
||||
* @namespace Log4js
|
||||
*/
|
||||
class Level {
|
||||
constructor(level, levelStr) {
|
||||
this.level = level;
|
||||
this.levelStr = levelStr;
|
||||
}
|
||||
|
||||
toString() {
|
||||
return this.levelStr;
|
||||
}
|
||||
|
||||
isLessThanOrEqualTo(otherLevel) {
|
||||
if (typeof otherLevel === 'string') {
|
||||
otherLevel = toLevel(otherLevel);
|
||||
}
|
||||
return this.level <= otherLevel.level;
|
||||
}
|
||||
|
||||
isGreaterThanOrEqualTo(otherLevel) {
|
||||
if (typeof otherLevel === 'string') {
|
||||
otherLevel = toLevel(otherLevel);
|
||||
}
|
||||
return this.level >= otherLevel.level;
|
||||
}
|
||||
|
||||
isEqualTo(otherLevel) {
|
||||
if (typeof otherLevel === 'string') {
|
||||
otherLevel = toLevel(otherLevel);
|
||||
}
|
||||
return this.level === otherLevel.level;
|
||||
}
|
||||
|
||||
function Level(level, levelStr) {
|
||||
this.level = level;
|
||||
this.levelStr = levelStr;
|
||||
}
|
||||
|
||||
/**
|
||||
* converts given String to corresponding Level
|
||||
* @param {String} sArg String value of Level OR Log4js.Level
|
||||
* @param {Log4js.Level} defaultLevel default Level, if no String representation
|
||||
* @return Level object
|
||||
* @type Log4js.Level
|
||||
* @param {Level|String} sArg -- String value of Level OR Log4js.Level
|
||||
* @param {Level} [defaultLevel] -- default Level, if no String representation
|
||||
* @return {Level}
|
||||
*/
|
||||
function toLevel(sArg, defaultLevel) {
|
||||
if (!sArg) {
|
||||
return defaultLevel;
|
||||
}
|
||||
|
||||
if (sArg instanceof Level) {
|
||||
module.exports[sArg.toString()] = sArg;
|
||||
return sArg;
|
||||
}
|
||||
if (typeof sArg === "string") {
|
||||
|
||||
if (typeof sArg === 'string') {
|
||||
return module.exports[sArg.toUpperCase()] || defaultLevel;
|
||||
}
|
||||
|
||||
return toLevel(sArg.toString());
|
||||
}
|
||||
|
||||
Level.prototype.toString = function () {
|
||||
return this.levelStr;
|
||||
};
|
||||
|
||||
Level.prototype.isLessThanOrEqualTo = function (otherLevel) {
|
||||
if (typeof otherLevel === "string") {
|
||||
otherLevel = toLevel(otherLevel);
|
||||
function getLevel(levelStr) {
|
||||
let level;
|
||||
if (typeof levelStr === 'string') {
|
||||
const levelUpper = levelStr.toUpperCase();
|
||||
level = toLevel(levelUpper);
|
||||
}
|
||||
return this.level <= otherLevel.level;
|
||||
};
|
||||
|
||||
Level.prototype.isGreaterThanOrEqualTo = function (otherLevel) {
|
||||
if (typeof otherLevel === "string") {
|
||||
otherLevel = toLevel(otherLevel);
|
||||
}
|
||||
return this.level >= otherLevel.level;
|
||||
};
|
||||
|
||||
Level.prototype.isEqualTo = function (otherLevel) {
|
||||
if (typeof otherLevel === "string") {
|
||||
otherLevel = toLevel(otherLevel);
|
||||
}
|
||||
return this.level === otherLevel.level;
|
||||
};
|
||||
return level;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
ALL: new Level(Number.MIN_VALUE, "ALL"),
|
||||
TRACE: new Level(5000, "TRACE"),
|
||||
DEBUG: new Level(10000, "DEBUG"),
|
||||
INFO: new Level(20000, "INFO"),
|
||||
WARN: new Level(30000, "WARN"),
|
||||
ERROR: new Level(40000, "ERROR"),
|
||||
FATAL: new Level(50000, "FATAL"),
|
||||
MARK: new Level(9007199254740992, "MARK"), // 2^53
|
||||
OFF: new Level(Number.MAX_VALUE, "OFF"),
|
||||
ALL: new Level(Number.MIN_VALUE, 'ALL'),
|
||||
TRACE: new Level(5000, 'TRACE'),
|
||||
DEBUG: new Level(10000, 'DEBUG'),
|
||||
INFO: new Level(20000, 'INFO'),
|
||||
WARN: new Level(30000, 'WARN'),
|
||||
ERROR: new Level(40000, 'ERROR'),
|
||||
FATAL: new Level(50000, 'FATAL'),
|
||||
MARK: new Level(9007199254740992, 'MARK'), // 2^53
|
||||
OFF: new Level(Number.MAX_VALUE, 'OFF'),
|
||||
toLevel: toLevel,
|
||||
Level: Level
|
||||
Level: Level,
|
||||
getLevel: getLevel
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user