logger.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. "use strict";
  2. /* eslint-disable prefer-rest-params */
  3. Object.defineProperty(exports, "__esModule", { value: true });
  4. exports.getArrow = exports.getInstance = void 0;
  5. const util = require("util");
  6. let loggerInstance;
  7. const defaultProvider = {
  8. // tslint:disable: no-console
  9. log: console.log,
  10. debug: console.log,
  11. info: console.info,
  12. warn: console.warn,
  13. error: console.error,
  14. };
  15. // log level 'weight'
  16. var LEVELS;
  17. (function (LEVELS) {
  18. LEVELS[LEVELS["debug"] = 10] = "debug";
  19. LEVELS[LEVELS["info"] = 20] = "info";
  20. LEVELS[LEVELS["warn"] = 30] = "warn";
  21. LEVELS[LEVELS["error"] = 50] = "error";
  22. LEVELS[LEVELS["silent"] = 80] = "silent";
  23. })(LEVELS || (LEVELS = {}));
  24. function getInstance() {
  25. if (!loggerInstance) {
  26. loggerInstance = new Logger();
  27. }
  28. return loggerInstance;
  29. }
  30. exports.getInstance = getInstance;
  31. class Logger {
  32. constructor() {
  33. this.setLevel('info');
  34. this.setProvider(() => defaultProvider);
  35. }
  36. // log will log messages, regardless of logLevels
  37. log() {
  38. this.provider.log(this._interpolate.apply(null, arguments));
  39. }
  40. debug() {
  41. if (this._showLevel('debug')) {
  42. this.provider.debug(this._interpolate.apply(null, arguments));
  43. }
  44. }
  45. info() {
  46. if (this._showLevel('info')) {
  47. this.provider.info(this._interpolate.apply(null, arguments));
  48. }
  49. }
  50. warn() {
  51. if (this._showLevel('warn')) {
  52. this.provider.warn(this._interpolate.apply(null, arguments));
  53. }
  54. }
  55. error() {
  56. if (this._showLevel('error')) {
  57. this.provider.error(this._interpolate.apply(null, arguments));
  58. }
  59. }
  60. setLevel(v) {
  61. if (this.isValidLevel(v)) {
  62. this.logLevel = v;
  63. }
  64. }
  65. setProvider(fn) {
  66. if (fn && this.isValidProvider(fn)) {
  67. this.provider = fn(defaultProvider);
  68. }
  69. }
  70. isValidProvider(fnProvider) {
  71. const result = true;
  72. if (fnProvider && typeof fnProvider !== 'function') {
  73. throw new Error('[HPM] Log provider config error. Expecting a function.');
  74. }
  75. return result;
  76. }
  77. isValidLevel(levelName) {
  78. const validLevels = Object.keys(LEVELS);
  79. const isValid = validLevels.includes(levelName);
  80. if (!isValid) {
  81. throw new Error('[HPM] Log level error. Invalid logLevel.');
  82. }
  83. return isValid;
  84. }
  85. /**
  86. * Decide to log or not to log, based on the log levels 'weight'
  87. * @param {String} showLevel [debug, info, warn, error, silent]
  88. * @return {Boolean}
  89. */
  90. _showLevel(showLevel) {
  91. let result = false;
  92. const currentLogLevel = LEVELS[this.logLevel];
  93. if (currentLogLevel && currentLogLevel <= LEVELS[showLevel]) {
  94. result = true;
  95. }
  96. return result;
  97. }
  98. // make sure logged messages and its data are return interpolated
  99. // make it possible for additional log data, such date/time or custom prefix.
  100. _interpolate(format, ...args) {
  101. const result = util.format(format, ...args);
  102. return result;
  103. }
  104. }
  105. /**
  106. * -> normal proxy
  107. * => router
  108. * ~> pathRewrite
  109. * ≈> router + pathRewrite
  110. *
  111. * @param {String} originalPath
  112. * @param {String} newPath
  113. * @param {String} originalTarget
  114. * @param {String} newTarget
  115. * @return {String}
  116. */
  117. function getArrow(originalPath, newPath, originalTarget, newTarget) {
  118. const arrow = ['>'];
  119. const isNewTarget = originalTarget !== newTarget; // router
  120. const isNewPath = originalPath !== newPath; // pathRewrite
  121. if (isNewPath && !isNewTarget) {
  122. arrow.unshift('~');
  123. }
  124. else if (!isNewPath && isNewTarget) {
  125. arrow.unshift('=');
  126. }
  127. else if (isNewPath && isNewTarget) {
  128. arrow.unshift('≈');
  129. }
  130. else {
  131. arrow.unshift('-');
  132. }
  133. return arrow.join('');
  134. }
  135. exports.getArrow = getArrow;