level.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var cache_getters_1 = require("../utils/cache-getters");
  4. var LogLevels = cache_getters_1.cacheGetters({
  5. trace: 10,
  6. debug: 20,
  7. info: 30,
  8. warn: 40,
  9. error: 50,
  10. fatal: 60,
  11. get lower() {
  12. return LogLevels.trace;
  13. },
  14. get higher() {
  15. return LogLevels.fatal;
  16. },
  17. }, 'lower', 'higher');
  18. exports.LogLevels = LogLevels;
  19. var LogLevelNames = ['trace', 'debug', 'info', 'warn', 'error', 'fatal'];
  20. exports.LogLevelNames = LogLevelNames;
  21. var LogLevelValues = LogLevelNames.map(function (name) { return LogLevels[name]; });
  22. exports.LogLevelValues = LogLevelValues;
  23. var LogLevelsScale = LogLevelNames.map(function (name, index, _a) {
  24. var length = _a.length;
  25. var first = index === 0;
  26. var last = index === length - 1;
  27. var from = first ? -Infinity : LogLevelValues[index];
  28. var next = last ? +Infinity : LogLevelValues[index + 1];
  29. var test;
  30. if (first) {
  31. test = function (level) { return level < next; };
  32. }
  33. else if (last) {
  34. test = function (level) { return level >= from; };
  35. }
  36. else {
  37. test = function (level) { return level < next && level >= from; };
  38. }
  39. return { range: { from: from, next: next }, name: name, test: test };
  40. });
  41. exports.LogLevelsScale = LogLevelsScale;
  42. var logLevelNameFor = function (level) {
  43. if (level == null || isNaN(level)) {
  44. return LogLevelNames[0];
  45. }
  46. return LogLevelsScale.find(function (_a) {
  47. var test = _a.test;
  48. return test(level);
  49. }).name;
  50. };
  51. exports.logLevelNameFor = logLevelNameFor;
  52. var parseLogLevel = function (level) {
  53. if (typeof level === 'string') {
  54. level = level.toLowerCase();
  55. if (level in LogLevels) {
  56. return LogLevels[level];
  57. }
  58. return /^\s*[0-9]+\s*$/.test(level) ? parseInt(level.trim(), 10) : undefined;
  59. }
  60. return typeof level === 'number' && !isNaN(level) ? +level : undefined;
  61. };
  62. exports.parseLogLevel = parseLogLevel;