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