levels.js 4.0 KB

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