@wutiange/log-listener-plugin 1.3.0-alpha.0 → 1.3.0-alpha.2
Sign up to get free protection for your applications and to get access to all the features.
- package/LICENSE +201 -201
- package/README.md +2 -2
- package/dist/server.d.ts +10 -0
- package/dist/server.js +45 -0
- package/dist/server.js.map +1 -0
- package/dist/src/HTTPInterceptor.d.ts +48 -0
- package/dist/src/HTTPInterceptor.js +202 -0
- package/dist/src/HTTPInterceptor.js.map +1 -0
- package/dist/src/logPlugin.d.ts +19 -20
- package/dist/src/logPlugin.js +108 -122
- package/dist/src/logPlugin.js.map +1 -1
- package/package.json +47 -47
- package/src/HTTPInterceptor.ts +319 -0
- package/src/__tests__/console.test.ts +25 -25
- package/src/common.ts +4 -4
- package/src/logPlugin.ts +238 -229
- package/src/server.ts +66 -66
- package/src/utils.ts +47 -47
- package/dist/packages/network-logger/Logger.d.ts +0 -28
- package/dist/packages/network-logger/Logger.js +0 -192
- package/dist/packages/network-logger/Logger.js.map +0 -1
- package/dist/packages/network-logger/NetworkRequestInfo.d.ts +0 -36
- package/dist/packages/network-logger/NetworkRequestInfo.js +0 -129
- package/dist/packages/network-logger/NetworkRequestInfo.js.map +0 -1
- package/dist/packages/network-logger/constant.d.ts +0 -2
- package/dist/packages/network-logger/constant.js +0 -6
- package/dist/packages/network-logger/constant.js.map +0 -1
- package/dist/packages/network-logger/types.d.ts +0 -14
- package/dist/packages/network-logger/types.js +0 -3
- package/dist/packages/network-logger/types.js.map +0 -1
- package/dist/packages/network-logger/utils/debounce.d.ts +0 -2
- package/dist/packages/network-logger/utils/debounce.js +0 -20
- package/dist/packages/network-logger/utils/debounce.js.map +0 -1
- package/dist/packages/network-logger/utils/extractHost.d.ts +0 -2
- package/dist/packages/network-logger/utils/extractHost.js +0 -9
- package/dist/packages/network-logger/utils/extractHost.js.map +0 -1
- package/dist/packages/network-logger/utils/fromEntries.d.ts +0 -2
- package/dist/packages/network-logger/utils/fromEntries.js +0 -8
- package/dist/packages/network-logger/utils/fromEntries.js.map +0 -1
- package/dist/packages/network-logger/utils/logger.d.ts +0 -1
- package/dist/packages/network-logger/utils/logger.js +0 -6
- package/dist/packages/network-logger/utils/logger.js.map +0 -1
- package/dist/src/CompatibilityManager.d.ts +0 -27
- package/dist/src/CompatibilityManager.js +0 -74
- package/dist/src/CompatibilityManager.js.map +0 -1
- package/dist/src/console.d.ts +0 -1
- package/dist/src/console.js +0 -20
- package/dist/src/console.js.map +0 -1
- package/dist/src/fetch.d.ts +0 -1
- package/dist/src/fetch.js +0 -48
- package/dist/src/fetch.js.map +0 -1
- package/dist/src/index.d.ts +0 -2
- package/dist/src/index.js +0 -8
- package/dist/src/index.js.map +0 -1
- package/packages/network-logger/Logger.ts +0 -274
- package/packages/network-logger/NetworkRequestInfo.ts +0 -133
- package/packages/network-logger/constant.ts +0 -3
- package/packages/network-logger/types.ts +0 -36
- package/packages/network-logger/utils/debounce.ts +0 -21
- package/packages/network-logger/utils/extractHost.ts +0 -7
- package/packages/network-logger/utils/fromEntries.ts +0 -7
- package/packages/network-logger/utils/logger.ts +0 -2
- package/src/CompatibilityManager.ts +0 -64
package/src/logPlugin.ts
CHANGED
@@ -1,229 +1,238 @@
|
|
1
|
-
import Server from './server';
|
2
|
-
import
|
3
|
-
import
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
private
|
9
|
-
private
|
10
|
-
private
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
this.
|
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
|
-
console.
|
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
|
-
this.
|
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
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
1
|
+
import Server from './server';
|
2
|
+
import { extractDomain } from './utils';
|
3
|
+
import { httpInterceptor } from './HTTPInterceptor';
|
4
|
+
|
5
|
+
class LogPlugin {
|
6
|
+
private server: Server | null = null;
|
7
|
+
private baseData: Record<string, any> = {};
|
8
|
+
private timeout: number | null = null;
|
9
|
+
private host = '';
|
10
|
+
private isAuto = false
|
11
|
+
|
12
|
+
auto = () => {
|
13
|
+
if (this.host) {
|
14
|
+
this.startRecordNetwork();
|
15
|
+
this.startRecordLog();
|
16
|
+
}
|
17
|
+
this.isAuto = true
|
18
|
+
}
|
19
|
+
|
20
|
+
unAuto = () => {
|
21
|
+
this.stopRecordLog()
|
22
|
+
httpInterceptor.disable()
|
23
|
+
httpInterceptor.removeAllListener()
|
24
|
+
this.isAuto = false
|
25
|
+
}
|
26
|
+
|
27
|
+
startRecordLog = () => {
|
28
|
+
const common = require('./common')
|
29
|
+
console.log = (...data: any[]) => {
|
30
|
+
this.log(...data);
|
31
|
+
common.log(...data);
|
32
|
+
};
|
33
|
+
|
34
|
+
console.warn = (...data: any[]) => {
|
35
|
+
this.warn(...data);
|
36
|
+
common.warn(...data);
|
37
|
+
};
|
38
|
+
|
39
|
+
console.error = (...data: any[]) => {
|
40
|
+
this.error(...data);
|
41
|
+
common.error(...data);
|
42
|
+
};
|
43
|
+
}
|
44
|
+
|
45
|
+
stopRecordLog = () => {
|
46
|
+
const common = require('./common')
|
47
|
+
console.log = common.log
|
48
|
+
console.warn = common.warn
|
49
|
+
console.error = common.error
|
50
|
+
}
|
51
|
+
|
52
|
+
startRecordNetwork = () => {
|
53
|
+
httpInterceptor.addListener("send", (data) => {
|
54
|
+
this.server?.network({
|
55
|
+
...this.baseData,
|
56
|
+
url: data.url,
|
57
|
+
id: data.id,
|
58
|
+
method: data.method,
|
59
|
+
headers: data.requestHeaders,
|
60
|
+
body: data.requestData,
|
61
|
+
createTime: data.startTime
|
62
|
+
})
|
63
|
+
})
|
64
|
+
httpInterceptor.addListener("response", (data) => {
|
65
|
+
this.server?.network({
|
66
|
+
...this.baseData,
|
67
|
+
headers: data.responseHeaders,
|
68
|
+
body: data.responseData,
|
69
|
+
requestId: data.id,
|
70
|
+
statusCode: data.status,
|
71
|
+
endTime: data.endTime
|
72
|
+
})
|
73
|
+
})
|
74
|
+
httpInterceptor.enable({
|
75
|
+
ignoredHosts: [extractDomain(this.host)]
|
76
|
+
})
|
77
|
+
}
|
78
|
+
|
79
|
+
setBaseUrl = (url: string) => {
|
80
|
+
if (!url?.trim()) {
|
81
|
+
httpInterceptor.disable()
|
82
|
+
this.stopRecordLog()
|
83
|
+
return
|
84
|
+
}
|
85
|
+
this.host = url.includes("http") ? url : `http://${url}`;
|
86
|
+
if (this.server) {
|
87
|
+
this.server.updateUrl(url);
|
88
|
+
} else {
|
89
|
+
this.server = new Server(url);
|
90
|
+
}
|
91
|
+
if (this.isAuto) {
|
92
|
+
this.startRecordNetwork();
|
93
|
+
this.startRecordLog()
|
94
|
+
}
|
95
|
+
}
|
96
|
+
|
97
|
+
/**
|
98
|
+
* @deprecated 不需要手动上报,日志插件会自动收集日志
|
99
|
+
*/
|
100
|
+
setTimeout = (timeout: number) => {
|
101
|
+
if (typeof timeout === 'number') {
|
102
|
+
this.timeout = timeout;
|
103
|
+
this.server?.updateTimeout(this.timeout);
|
104
|
+
}
|
105
|
+
}
|
106
|
+
|
107
|
+
/**
|
108
|
+
* @deprecated 不需要手动上报,日志插件会自动收集日志
|
109
|
+
*/
|
110
|
+
getTimeout = () => {
|
111
|
+
if (typeof this.timeout === 'number') {
|
112
|
+
return this.timeout;
|
113
|
+
}
|
114
|
+
return null;
|
115
|
+
}
|
116
|
+
|
117
|
+
setBaseData = (data: Record<string, any> = {}) => {
|
118
|
+
this.baseData = data;
|
119
|
+
}
|
120
|
+
|
121
|
+
private _log = (level: string, tag: string, ...data: any[]) => {
|
122
|
+
const sendData = {
|
123
|
+
...this.baseData,
|
124
|
+
message: data,
|
125
|
+
tag,
|
126
|
+
level: level ?? 'log',
|
127
|
+
createTime: Date.now(),
|
128
|
+
};
|
129
|
+
this.server?.log(sendData);
|
130
|
+
}
|
131
|
+
|
132
|
+
tag = (tag: string, ...data: any[]) => {
|
133
|
+
this._log('log', tag, ...data);
|
134
|
+
}
|
135
|
+
|
136
|
+
log = (...data: any[]) => {
|
137
|
+
this._log('log', 'default', ...data);
|
138
|
+
}
|
139
|
+
|
140
|
+
warn = (...data: any[]) => {
|
141
|
+
this._log('warn', 'default', ...data);
|
142
|
+
}
|
143
|
+
|
144
|
+
error = (...data: any[]) => {
|
145
|
+
this._log('error', 'default', ...data);
|
146
|
+
}
|
147
|
+
|
148
|
+
/**
|
149
|
+
* @deprecated 不需要手动上报,日志插件会自动收集日志
|
150
|
+
*/
|
151
|
+
uniqueReq = async (
|
152
|
+
uniqueId: string | undefined,
|
153
|
+
input: RequestInfo | URL,
|
154
|
+
init?: RequestInit
|
155
|
+
) => {
|
156
|
+
let url: string | null = null;
|
157
|
+
let method = init?.method ?? 'get';
|
158
|
+
let headers = init?.headers;
|
159
|
+
let body = init?.body;
|
160
|
+
if (input instanceof Request) {
|
161
|
+
url = input.url;
|
162
|
+
method = input.method ?? 'get';
|
163
|
+
headers = (input.headers as Record<string, any>).map;
|
164
|
+
body = input.body;
|
165
|
+
} else if (input instanceof URL) {
|
166
|
+
url = input.href;
|
167
|
+
} else {
|
168
|
+
url = input;
|
169
|
+
}
|
170
|
+
return this.server?.network({
|
171
|
+
...this.baseData,
|
172
|
+
url,
|
173
|
+
id: uniqueId,
|
174
|
+
method,
|
175
|
+
headers,
|
176
|
+
body,
|
177
|
+
createTime: Date.now(),
|
178
|
+
});
|
179
|
+
}
|
180
|
+
|
181
|
+
private _res = async (uniqueId?: string, id?: number, response?: Response) => {
|
182
|
+
const body = await response?.text();
|
183
|
+
return this.server?.network({
|
184
|
+
...this.baseData,
|
185
|
+
headers: (response?.headers as Record<string, any>).map,
|
186
|
+
body,
|
187
|
+
requestId: uniqueId ?? Number(id),
|
188
|
+
statusCode: response?.status,
|
189
|
+
endTime: Date.now(),
|
190
|
+
});
|
191
|
+
}
|
192
|
+
|
193
|
+
/**
|
194
|
+
* @deprecated 不需要手动上报,日志插件会自动收集日志
|
195
|
+
*/
|
196
|
+
resTimeout = async (uniqueId: string) => {
|
197
|
+
return this.server?.network({
|
198
|
+
...this.baseData,
|
199
|
+
isTimeout: true,
|
200
|
+
requestId: uniqueId,
|
201
|
+
});
|
202
|
+
}
|
203
|
+
|
204
|
+
/**
|
205
|
+
* @deprecated 不需要手动上报,日志插件会自动收集日志
|
206
|
+
*/
|
207
|
+
resResponseError = async (uniqueId: string) => {
|
208
|
+
return this.server?.network({
|
209
|
+
...this.baseData,
|
210
|
+
isResponseError: true,
|
211
|
+
requestId: uniqueId,
|
212
|
+
});
|
213
|
+
}
|
214
|
+
|
215
|
+
/**
|
216
|
+
* @deprecated 不需要手动上报,日志插件会自动收集日志
|
217
|
+
*/
|
218
|
+
uniqueRes = async (uniqueId: string, response?: Response) => {
|
219
|
+
return this._res(uniqueId, undefined, response);
|
220
|
+
}
|
221
|
+
|
222
|
+
/**
|
223
|
+
* @deprecated 不需要手动上报,日志插件会自动收集日志
|
224
|
+
*/
|
225
|
+
req = async (input: RequestInfo | URL, init?: RequestInit) => {
|
226
|
+
return this.uniqueReq(undefined, input, init);
|
227
|
+
}
|
228
|
+
|
229
|
+
/**
|
230
|
+
* @deprecated 不需要手动上报,日志插件会自动收集日志
|
231
|
+
*/
|
232
|
+
res = async (id: number, response?: Response) => {
|
233
|
+
return this._res(undefined, id, response);
|
234
|
+
}
|
235
|
+
}
|
236
|
+
const logPlugin = new LogPlugin();
|
237
|
+
export { LogPlugin };
|
238
|
+
export default logPlugin;
|
package/src/server.ts
CHANGED
@@ -1,66 +1,66 @@
|
|
1
|
-
import {hasPort, sleep} from './utils';
|
2
|
-
const DEFAULT_PORT = 27751
|
3
|
-
class Server {
|
4
|
-
private baseUrl = '';
|
5
|
-
private timeout: number;
|
6
|
-
|
7
|
-
constructor(url: string, timeout: number = 3000) {
|
8
|
-
this.updateUrl(url);
|
9
|
-
this.timeout = timeout;
|
10
|
-
}
|
11
|
-
|
12
|
-
updateTimeout(timeout = 3000) {
|
13
|
-
this.timeout = timeout;
|
14
|
-
}
|
15
|
-
|
16
|
-
private getPort() {
|
17
|
-
if (hasPort(this.baseUrl)) {
|
18
|
-
return ''
|
19
|
-
}
|
20
|
-
return DEFAULT_PORT;
|
21
|
-
}
|
22
|
-
|
23
|
-
private async send(path: string, data: Record<string, any>) {
|
24
|
-
try {
|
25
|
-
if (!this.baseUrl) {
|
26
|
-
return null;
|
27
|
-
}
|
28
|
-
const common = require('./common');
|
29
|
-
const result = await Promise.race([
|
30
|
-
common.tempFetch(`${this.baseUrl}:${this.getPort()}/${path}`, {
|
31
|
-
method: 'POST',
|
32
|
-
headers: {
|
33
|
-
'Content-Type': 'application/json;charset=utf-8',
|
34
|
-
},
|
35
|
-
body: JSON.stringify(data, (_, val) => {
|
36
|
-
if (val instanceof Error) {
|
37
|
-
return val.toString();
|
38
|
-
}
|
39
|
-
return val;
|
40
|
-
}),
|
41
|
-
}),
|
42
|
-
sleep(this.timeout, true),
|
43
|
-
]);
|
44
|
-
if (result instanceof Response) {
|
45
|
-
return result.text();
|
46
|
-
}
|
47
|
-
return null;
|
48
|
-
} catch (error) {
|
49
|
-
return null;
|
50
|
-
}
|
51
|
-
}
|
52
|
-
|
53
|
-
updateUrl(url: string) {
|
54
|
-
this.baseUrl = url;
|
55
|
-
}
|
56
|
-
|
57
|
-
async log(data: Record<string, any>) {
|
58
|
-
return this.send('log', data);
|
59
|
-
}
|
60
|
-
|
61
|
-
async network(data: Record<string, any>) {
|
62
|
-
return this.send('network', data);
|
63
|
-
}
|
64
|
-
}
|
65
|
-
|
66
|
-
export default Server;
|
1
|
+
import {hasPort, sleep} from './utils';
|
2
|
+
const DEFAULT_PORT = 27751
|
3
|
+
class Server {
|
4
|
+
private baseUrl = '';
|
5
|
+
private timeout: number;
|
6
|
+
|
7
|
+
constructor(url: string, timeout: number = 3000) {
|
8
|
+
this.updateUrl(url);
|
9
|
+
this.timeout = timeout;
|
10
|
+
}
|
11
|
+
|
12
|
+
updateTimeout(timeout = 3000) {
|
13
|
+
this.timeout = timeout;
|
14
|
+
}
|
15
|
+
|
16
|
+
private getPort() {
|
17
|
+
if (hasPort(this.baseUrl)) {
|
18
|
+
return ''
|
19
|
+
}
|
20
|
+
return DEFAULT_PORT;
|
21
|
+
}
|
22
|
+
|
23
|
+
private async send(path: string, data: Record<string, any>) {
|
24
|
+
try {
|
25
|
+
if (!this.baseUrl) {
|
26
|
+
return null;
|
27
|
+
}
|
28
|
+
const common = require('./common');
|
29
|
+
const result = await Promise.race([
|
30
|
+
common.tempFetch(`${this.baseUrl}:${this.getPort()}/${path}`, {
|
31
|
+
method: 'POST',
|
32
|
+
headers: {
|
33
|
+
'Content-Type': 'application/json;charset=utf-8',
|
34
|
+
},
|
35
|
+
body: JSON.stringify(data, (_, val) => {
|
36
|
+
if (val instanceof Error) {
|
37
|
+
return val.toString();
|
38
|
+
}
|
39
|
+
return val;
|
40
|
+
}),
|
41
|
+
}),
|
42
|
+
sleep(this.timeout, true),
|
43
|
+
]);
|
44
|
+
if (result instanceof Response) {
|
45
|
+
return result.text();
|
46
|
+
}
|
47
|
+
return null;
|
48
|
+
} catch (error) {
|
49
|
+
return null;
|
50
|
+
}
|
51
|
+
}
|
52
|
+
|
53
|
+
updateUrl(url: string) {
|
54
|
+
this.baseUrl = url;
|
55
|
+
}
|
56
|
+
|
57
|
+
async log(data: Record<string, any>) {
|
58
|
+
return this.send('log', data);
|
59
|
+
}
|
60
|
+
|
61
|
+
async network(data: Record<string, any>) {
|
62
|
+
return this.send('network', data);
|
63
|
+
}
|
64
|
+
}
|
65
|
+
|
66
|
+
export default Server;
|