levels.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. "use strict";
  2. function Level(level, levelStr) {
  3. this.level = level;
  4. this.levelStr = levelStr;
  5. }
  6. /**
  7. * converts given String to corresponding Level
  8. * @param {String} sArg String value of Level OR Log4js.Level
  9. * @param {Log4js.Level} defaultLevel default Level, if no String representation
  10. * @return Level object
  11. * @type Log4js.Level
  12. */
  13. function toLevel(sArg, defaultLevel) {
  14. if (!sArg) {
  15. return defaultLevel;
  16. }
  17. if (typeof sArg == "string") {
  18. var s = sArg.toUpperCase();
  19. if (module.exports[s]) {
  20. return module.exports[s];
  21. } else {
  22. return defaultLevel;
  23. }
  24. }
  25. return toLevel(sArg.toString());
  26. }
  27. Level.prototype.toString = function() {
  28. return this.levelStr;
  29. };
  30. Level.prototype.isLessThanOrEqualTo = function(otherLevel) {
  31. if (typeof otherLevel === "string") {
  32. otherLevel = toLevel(otherLevel);
  33. }
  34. return this.level <= otherLevel.level;
  35. };
  36. Level.prototype.isGreaterThanOrEqualTo = function(otherLevel) {
  37. if (typeof otherLevel === "string") {
  38. otherLevel = toLevel(otherLevel);
  39. }
  40. return this.level >= otherLevel.level;
  41. };
  42. Level.prototype.isEqualTo = function(otherLevel) {
  43. if (typeof otherLevel == "string") {
  44. otherLevel = toLevel(otherLevel);
  45. }
  46. return this.level === otherLevel.level;
  47. };
  48. module.exports = {
  49. ALL: new Level(Number.MIN_VALUE, "ALL"),
  50. TRACE: new Level(5000, "TRACE"),
  51. DEBUG: new Level(10000, "DEBUG"),
  52. INFO: new Level(20000, "INFO"),
  53. WARN: new Level(30000, "WARN"),
  54. ERROR: new Level(40000, "ERROR"),
  55. FATAL: new Level(50000, "FATAL"),
  56. OFF: new Level(Number.MAX_VALUE, "OFF"),
  57. toLevel: toLevel
  58. };