-
Notifications
You must be signed in to change notification settings - Fork 869
/
Copy pathlogger.ts
157 lines (128 loc) · 3.38 KB
/
logger.ts
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* eslint-disable prefer-rest-params */
import * as util from 'util';
let loggerInstance;
const defaultProvider = {
// tslint:disable: no-console
log: console.log,
debug: console.log, // use .log(); since console does not have .debug()
info: console.info,
warn: console.warn,
error: console.error,
};
// log level 'weight'
enum LEVELS {
debug = 10,
info = 20,
warn = 30,
error = 50,
silent = 80,
}
export function getInstance() {
if (!loggerInstance) {
loggerInstance = new Logger();
}
return loggerInstance;
}
class Logger {
public logLevel;
public provider;
constructor() {
this.setLevel('info');
this.setProvider(() => defaultProvider);
}
// log will log messages, regardless of logLevels
public log() {
this.provider.log(this._interpolate.apply(null, arguments));
}
public debug() {
if (this._showLevel('debug')) {
this.provider.debug(this._interpolate.apply(null, arguments));
}
}
public info() {
if (this._showLevel('info')) {
this.provider.info(this._interpolate.apply(null, arguments));
}
}
public warn() {
if (this._showLevel('warn')) {
this.provider.warn(this._interpolate.apply(null, arguments));
}
}
public error() {
if (this._showLevel('error')) {
this.provider.error(this._interpolate.apply(null, arguments));
}
}
public setLevel(v) {
if (this.isValidLevel(v)) {
this.logLevel = v;
}
}
public setProvider(fn) {
if (fn && this.isValidProvider(fn)) {
this.provider = fn(defaultProvider);
}
}
public isValidProvider(fnProvider) {
const result = true;
if (fnProvider && typeof fnProvider !== 'function') {
throw new Error('[HPM] Log provider config error. Expecting a function.');
}
return result;
}
public 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}
*/
private _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.
private _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}
*/
export 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('');
}