forked from alibaba/lowcode-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.ts
More file actions
200 lines (186 loc) · 4.83 KB
/
logger.ts
File metadata and controls
200 lines (186 loc) · 4.83 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* eslint-disable no-console */
/* eslint-disable no-param-reassign */
import { isObject } from './is-object';
export type Level = 'debug' | 'log' | 'info' | 'warn' | 'error';
interface Options {
level: Level;
bizName: string;
}
const levels: Record<string, number> = {
debug: -1,
log: 0,
info: 0,
warn: 1,
error: 2,
};
const bizNameColors = [
'#daa569',
'#00ffff',
'#385e0f',
'#7fffd4',
'#00c957',
'#b0e0e6',
'#4169e1',
'#6a5acd',
'#87ceeb',
'#ffff00',
'#e3cf57',
'#ff9912',
'#eb8e55',
'#ffe384',
'#40e0d0',
'#a39480',
'#d2691e',
'#ff7d40',
'#f0e68c',
'#bc8f8f',
'#c76114',
'#734a12',
'#5e2612',
'#0000ff',
'#3d59ab',
'#1e90ff',
'#03a89e',
'#33a1c9',
'#a020f0',
'#a066d3',
'#da70d6',
'#dda0dd',
'#688e23',
'#2e8b57',
];
const bodyColors: Record<string, string> = {
debug: '#666666',
log: '#bbbbbb',
info: '#ffffff',
warn: '#bbbbbb',
error: '#bbbbbb',
};
const levelMarks: Record<string, string> = {
debug: 'debug',
log: 'log',
info: 'info',
warn: 'warn',
error: 'error',
};
const outputFuntion: Record<string, any> = {
debug: console.log,
log: console.log,
info: console.log,
warn: console.warn,
error: console.error,
};
const bizNameColorConfig: Record<string, string> = {};
const shouldOutput = (
logLevel: string,
targetLevel: string = 'warn',
bizName: string,
targetBizName: string,
): boolean => {
const isLevelFit = (levels as any)[targetLevel] <= (levels as any)[logLevel];
const isBizNameFit = targetBizName === '*' || bizName.indexOf(targetBizName) > -1;
return isLevelFit && isBizNameFit;
};
const output = (logLevel: string, bizName: string) => {
return (...args: any[]) => {
return outputFuntion[logLevel].apply(console, getLogArgs(args, bizName, logLevel));
};
};
const getColor = (bizName: string) => {
if (!bizNameColorConfig[bizName]) {
const color = bizNameColors[Object.keys(bizNameColorConfig).length % bizNameColors.length];
bizNameColorConfig[bizName] = color;
}
return bizNameColorConfig[bizName];
};
const getLogArgs = (args: any, bizName: string, logLevel: string) => {
const color = getColor(bizName);
const bodyColor = bodyColors[logLevel];
const argsArray = args[0];
let prefix = `%c[${bizName}]%c[${levelMarks[logLevel]}]:`;
argsArray.forEach((arg: any) => {
if (isObject(arg)) {
prefix += '%o';
} else {
prefix += '%s';
}
});
let processedArgs = [prefix, `color: ${color}`, `color: ${bodyColor}`];
processedArgs = processedArgs.concat(argsArray);
return processedArgs;
};
const parseLogConf = (logConf: string, options: Options): { level: string; bizName: string} => {
if (!logConf) {
return {
level: options.level,
bizName: options.bizName,
};
}
if (logConf.indexOf(':') > -1) {
const pair = logConf.split(':');
return {
level: pair[0],
bizName: pair[1] || '*',
};
}
return {
level: logConf,
bizName: '*',
};
};
const defaultOptions: Options = {
level: 'warn',
bizName: '*',
};
class Logger {
bizName: string;
targetBizName: string;
targetLevel: string;
constructor(options: Options) {
options = { ...defaultOptions, ...options };
const _location = location || {} as any;
// __logConf__ 格式为 logLevel[:bizName], bizName is used as: targetBizName like '%bizName%'
// 1. __logConf__=log or __logConf__=warn, etc.
// 2. __logConf__=log:* or __logConf__=warn:*, etc.
// 2. __logConf__=log:bizName or __logConf__=warn:partOfBizName, etc.
const logConf = (((/__(?:logConf|logLevel)__=([^#/&]*)/.exec(_location.href)) || [])[1]);
const targetOptions = parseLogConf(logConf, options);
this.bizName = options.bizName;
this.targetBizName = targetOptions.bizName;
this.targetLevel = targetOptions.level;
}
debug(...args: any[]): void {
if (!shouldOutput('debug', this.targetLevel, this.bizName, this.targetBizName)) {
return;
}
return output('debug', this.bizName)(args);
}
log(...args: any[]): void {
if (!shouldOutput('log', this.targetLevel, this.bizName, this.targetBizName)) {
return;
}
return output('log', this.bizName)(args);
}
info(...args: any[]): void {
if (!shouldOutput('info', this.targetLevel, this.bizName, this.targetBizName)) {
return;
}
return output('info', this.bizName)(args);
}
warn(...args: any[]): void {
if (!shouldOutput('warn', this.targetLevel, this.bizName, this.targetBizName)) {
return;
}
return output('warn', this.bizName)(args);
}
error(...args: any[]): void {
if (!shouldOutput('error', this.targetLevel, this.bizName, this.targetBizName)) {
return;
}
return output('error', this.bizName)(args);
}
}
export { Logger };
export function getLogger(config: { level: Level; bizName: string }): Logger {
return new Logger(config);
}