levels.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. if (typeof sArg === 'string') {
  31. return Level[sArg.toUpperCase()] || defaultLevel;
  32. }
  33. return Level.getLevel(sArg.toString());
  34. }
  35. static addLevels(customLevels) {
  36. if (customLevels) {
  37. const levels = Object.keys(customLevels);
  38. levels.forEach((l) => {
  39. Level[l.toUpperCase()] = new Level(
  40. customLevels[l].value,
  41. l.toUpperCase(),
  42. customLevels[l].colour
  43. );
  44. Level.levels.push(Level[l.toUpperCase()]);
  45. });
  46. Level.levels.sort((a, b) => a.level - b.level);
  47. }
  48. }
  49. isLessThanOrEqualTo(otherLevel) {
  50. if (typeof otherLevel === 'string') {
  51. otherLevel = Level.getLevel(otherLevel);
  52. }
  53. return this.level <= otherLevel.level;
  54. }
  55. isGreaterThanOrEqualTo(otherLevel) {
  56. if (typeof otherLevel === 'string') {
  57. otherLevel = Level.getLevel(otherLevel);
  58. }
  59. return this.level >= otherLevel.level;
  60. }
  61. isEqualTo(otherLevel) {
  62. if (typeof otherLevel === 'string') {
  63. otherLevel = Level.getLevel(otherLevel);
  64. }
  65. return this.level === otherLevel.level;
  66. }
  67. }
  68. Level.levels = [];
  69. Level.addLevels({
  70. ALL: { value: Number.MIN_VALUE, colour: 'grey' },
  71. TRACE: { value: 5000, colour: 'blue' },
  72. DEBUG: { value: 10000, colour: 'cyan' },
  73. INFO: { value: 20000, colour: 'green' },
  74. WARN: { value: 30000, colour: 'yellow' },
  75. ERROR: { value: 40000, colour: 'red' },
  76. FATAL: { value: 50000, colour: 'magenta' },
  77. MARK: { value: 9007199254740992, colour: 'grey' }, // 2^53
  78. OFF: { value: Number.MAX_VALUE, colour: 'grey' }
  79. });
  80. configuration.addListener((config) => {
  81. const levelConfig = config.levels;
  82. if (levelConfig) {
  83. configuration.throwExceptionIf(
  84. config,
  85. configuration.not(configuration.anObject(levelConfig)),
  86. 'levels must be an object'
  87. );
  88. const newLevels = Object.keys(levelConfig);
  89. newLevels.forEach((l) => {
  90. configuration.throwExceptionIf(
  91. config,
  92. configuration.not(configuration.validIdentifier(l)),
  93. `level name "${l}" is not a valid identifier (must start with a letter, only contain A-Z,a-z,0-9,_)`
  94. );
  95. configuration.throwExceptionIf(
  96. config,
  97. configuration.not(configuration.anObject(levelConfig[l])),
  98. `level "${l}" must be an object`
  99. );
  100. configuration.throwExceptionIf(
  101. config,
  102. configuration.not(levelConfig[l].value),
  103. `level "${l}" must have a 'value' property`
  104. );
  105. configuration.throwExceptionIf(
  106. config,
  107. configuration.not(configuration.anInteger(levelConfig[l].value)),
  108. `level "${l}".value must have an integer value`
  109. );
  110. configuration.throwExceptionIf(
  111. config,
  112. configuration.not(levelConfig[l].colour),
  113. `level "${l}" must have a 'colour' property`
  114. );
  115. configuration.throwExceptionIf(
  116. config,
  117. configuration.not(validColours.indexOf(levelConfig[l].colour) > -1),
  118. `level "${l}".colour must be one of ${validColours.join(', ')}`
  119. );
  120. });
  121. }
  122. });
  123. configuration.addListener((config) => {
  124. Level.addLevels(config.levels);
  125. });
  126. module.exports = Level;