levels.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. const configuration = require('./configuration');
  2. const validColours = [
  3. 'white',
  4. 'grey',
  5. 'black',
  6. 'blue',
  7. 'cyan',
  8. 'green',
  9. 'magenta',
  10. 'red',
  11. 'yellow',
  12. ];
  13. class Level {
  14. constructor(level, levelStr, colour) {
  15. this.level = level;
  16. this.levelStr = levelStr;
  17. this.colour = colour;
  18. }
  19. toString() {
  20. return this.levelStr;
  21. }
  22. /**
  23. * converts given String to corresponding Level
  24. * @param {(Level|string)} sArg -- String value of Level OR Log4js.Level
  25. * @param {Level} [defaultLevel] -- default Level, if no String representation
  26. * @return {Level}
  27. */
  28. static getLevel(sArg, defaultLevel) {
  29. if (!sArg) {
  30. return defaultLevel;
  31. }
  32. if (sArg instanceof Level) {
  33. return sArg;
  34. }
  35. // a json-serialised level won't be an instance of Level (see issue #768)
  36. if (sArg instanceof Object && sArg.levelStr) {
  37. sArg = sArg.levelStr;
  38. }
  39. return Level[sArg.toString().toUpperCase()] || defaultLevel;
  40. }
  41. static addLevels(customLevels) {
  42. if (customLevels) {
  43. const levels = Object.keys(customLevels);
  44. levels.forEach((l) => {
  45. const levelStr = l.toUpperCase();
  46. Level[levelStr] = new Level(
  47. customLevels[l].value,
  48. levelStr,
  49. customLevels[l].colour
  50. );
  51. const existingLevelIndex = Level.levels.findIndex(
  52. (lvl) => lvl.levelStr === levelStr
  53. );
  54. if (existingLevelIndex > -1) {
  55. Level.levels[existingLevelIndex] = Level[levelStr];
  56. } else {
  57. Level.levels.push(Level[levelStr]);
  58. }
  59. });
  60. Level.levels.sort((a, b) => a.level - b.level);
  61. }
  62. }
  63. isLessThanOrEqualTo(otherLevel) {
  64. if (typeof otherLevel === 'string') {
  65. otherLevel = Level.getLevel(otherLevel);
  66. }
  67. return this.level <= otherLevel.level;
  68. }
  69. isGreaterThanOrEqualTo(otherLevel) {
  70. if (typeof otherLevel === 'string') {
  71. otherLevel = Level.getLevel(otherLevel);
  72. }
  73. return this.level >= otherLevel.level;
  74. }
  75. isEqualTo(otherLevel) {
  76. if (typeof otherLevel === 'string') {
  77. otherLevel = Level.getLevel(otherLevel);
  78. }
  79. return this.level === otherLevel.level;
  80. }
  81. }
  82. Level.levels = [];
  83. Level.addLevels({
  84. ALL: { value: Number.MIN_VALUE, colour: 'grey' },
  85. TRACE: { value: 5000, colour: 'blue' },
  86. DEBUG: { value: 10000, colour: 'cyan' },
  87. INFO: { value: 20000, colour: 'green' },
  88. WARN: { value: 30000, colour: 'yellow' },
  89. ERROR: { value: 40000, colour: 'red' },
  90. FATAL: { value: 50000, colour: 'magenta' },
  91. MARK: { value: 9007199254740992, colour: 'grey' }, // 2^53
  92. OFF: { value: Number.MAX_VALUE, colour: 'grey' },
  93. });
  94. configuration.addListener((config) => {
  95. const levelConfig = config.levels;
  96. if (levelConfig) {
  97. configuration.throwExceptionIf(
  98. config,
  99. configuration.not(configuration.anObject(levelConfig)),
  100. 'levels must be an object'
  101. );
  102. const newLevels = Object.keys(levelConfig);
  103. newLevels.forEach((l) => {
  104. configuration.throwExceptionIf(
  105. config,
  106. configuration.not(configuration.validIdentifier(l)),
  107. `level name "${l}" is not a valid identifier (must start with a letter, only contain A-Z,a-z,0-9,_)`
  108. );
  109. configuration.throwExceptionIf(
  110. config,
  111. configuration.not(configuration.anObject(levelConfig[l])),
  112. `level "${l}" must be an object`
  113. );
  114. configuration.throwExceptionIf(
  115. config,
  116. configuration.not(levelConfig[l].value),
  117. `level "${l}" must have a 'value' property`
  118. );
  119. configuration.throwExceptionIf(
  120. config,
  121. configuration.not(configuration.anInteger(levelConfig[l].value)),
  122. `level "${l}".value must have an integer value`
  123. );
  124. configuration.throwExceptionIf(
  125. config,
  126. configuration.not(levelConfig[l].colour),
  127. `level "${l}" must have a 'colour' property`
  128. );
  129. configuration.throwExceptionIf(
  130. config,
  131. configuration.not(validColours.indexOf(levelConfig[l].colour) > -1),
  132. `level "${l}".colour must be one of ${validColours.join(', ')}`
  133. );
  134. });
  135. }
  136. });
  137. configuration.addListener((config) => {
  138. Level.addLevels(config.levels);
  139. });
  140. module.exports = Level;