levels.js 4.1 KB

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