From bdeb7b9c41719cd801bed2ce69fcf700b1929201 Mon Sep 17 00:00:00 2001 From: e-cloud Date: Thu, 14 Jul 2016 22:06:01 +0800 Subject: [PATCH] refactor: level.js pick the getLevel function back from log4js.js --- lib/levels.js | 103 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 61 insertions(+), 42 deletions(-) diff --git a/lib/levels.js b/lib/levels.js index 9ace363..2d981ac 100644 --- a/lib/levels.js +++ b/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 };