-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcreateConsoleLogger.ts
More file actions
174 lines (169 loc) · 4.85 KB
/
createConsoleLogger.ts
File metadata and controls
174 lines (169 loc) · 4.85 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
/**
* The following code is modified based on
* https://github.com/webpack/webpack-dev-server
*
* MIT Licensed
* Author Tobias Koppers @sokra
* Copyright (c) JS Foundation and other contributors
* https://github.com/webpack/webpack-dev-server/blob/main/LICENSE
*/
import {
LogType,
type Args,
type FilterFunction,
type FilterItemTypes,
type LoggerOptions,
type LoggingFunction,
type LogTypeEnum,
} from '../types';
const filterToFunction = (
item: FilterItemTypes,
): FilterFunction | undefined => {
if (typeof item === 'string') {
const regExp = new RegExp(
`[\\\\/]${item.replace(/[-[\]{}()*+?.\\^$|]/g, '\\$&')}([\\\\/]|$|!|\\?)`,
);
return (ident) => regExp.test(ident);
}
if (item && typeof item === 'object' && typeof item.test === 'function') {
return (ident) => item.test(ident);
}
if (typeof item === 'function') {
return item;
}
if (typeof item === 'boolean') {
return () => item;
}
};
const LogLevel: Record<string, number> = {
none: 6,
false: 6,
error: 5,
warn: 4,
info: 3,
log: 2,
true: 2,
verbose: 1,
};
export default ({
level = 'info',
debug = false,
console,
}: LoggerOptions): LoggingFunction => {
const debugFilters = (
typeof debug === 'boolean'
? [() => debug]
: [...(Array.isArray(debug) ? debug : [debug])].map(filterToFunction)
) as FilterFunction[];
const loglevel = LogLevel[`${level}`] || 0;
const logger = (name: string, type: LogTypeEnum, args?: Args): void => {
const labeledArgs = () => {
if (Array.isArray(args)) {
if (args.length > 0 && typeof args[0] === 'string') {
return [`[${name}] ${args[0]}`, ...args.slice(1)];
}
return [`[${name}]`, ...args];
}
return [];
};
const debug = debugFilters.some((f) => f(name));
switch (type) {
case LogType.debug:
if (!debug) return;
if (typeof console.debug === 'function') {
console.debug(...labeledArgs());
} else {
console.log(...labeledArgs());
}
break;
case LogType.log:
if (!debug && loglevel > LogLevel.log) return;
console.log(...labeledArgs());
break;
case LogType.info:
if (!debug && loglevel > LogLevel.info) return;
console.info(...labeledArgs());
break;
case LogType.warn:
if (!debug && loglevel > LogLevel.warn) return;
console.warn(...labeledArgs());
break;
case LogType.error:
if (!debug && loglevel > LogLevel.error) return;
console.error(...labeledArgs());
break;
case LogType.trace:
if (!debug) return;
console.trace();
break;
case LogType.groupCollapsed:
if (!debug && loglevel > LogLevel.log) return;
if (!debug && loglevel > LogLevel.verbose) {
if (typeof console.groupCollapsed === 'function') {
console.groupCollapsed(...labeledArgs());
} else {
console.log(...labeledArgs());
}
break;
}
// falls through
case LogType.group:
if (!debug && loglevel > LogLevel.log) return;
if (typeof console.group === 'function') {
console.group(...labeledArgs());
} else {
console.log(...labeledArgs());
}
break;
case LogType.groupEnd:
if (!debug && loglevel > LogLevel.log) return;
if (typeof console.groupEnd === 'function') {
console.groupEnd();
}
break;
case LogType.time: {
if (!debug && loglevel > LogLevel.log) return;
const [label, start, end] = args as [string, number, number];
const ms = start * 1000 + end / 1000000;
const msg = `[${name}] ${label}: ${ms} ms`;
if (typeof console.logTime === 'function') {
console.logTime(msg);
} else {
console.log(msg);
}
break;
}
case LogType.profile:
if (typeof console.profile === 'function') {
console.profile(...labeledArgs());
}
break;
case LogType.profileEnd:
if (typeof console.profileEnd === 'function') {
console.profileEnd(...labeledArgs());
}
break;
case LogType.clear:
if (!debug && loglevel > LogLevel.log) return;
if (typeof console.clear === 'function') {
console.clear();
}
break;
case LogType.status:
if (!debug && loglevel > LogLevel.info) return;
if (typeof console.status === 'function') {
if (!args || args.length === 0) {
console.status();
} else {
console.status(...labeledArgs());
}
} else if (args && args.length !== 0) {
console.info(...labeledArgs());
}
break;
default:
throw new Error(`Unexpected LogType ${type}`);
}
};
return logger;
};