@wutiange/log-listener-plugin 2.0.1-alpha.3 → 2.0.1
Sign up to get free protection for your applications and to get access to all the features.
- package/LICENSE +201 -201
- package/README.md +163 -163
- package/dist/src/HTTPInterceptor.js +2 -1
- package/dist/src/HTTPInterceptor.js.map +1 -1
- package/dist/src/__tests__/Server.test.js +4 -4
- package/dist/src/__tests__/Server.test.js.map +1 -1
- package/dist/src/__tests__/console.test.d.ts +1 -0
- package/dist/src/__tests__/console.test.js +29 -0
- package/dist/src/__tests__/console.test.js.map +1 -0
- package/dist/src/__tests__/utils.test.js +5 -1
- package/dist/src/__tests__/utils.test.js.map +1 -1
- package/dist/src/common.d.ts +1 -7
- package/dist/src/common.js +2 -14
- package/dist/src/common.js.map +1 -1
- package/dist/src/logPlugin.js +22 -17
- package/dist/src/logPlugin.js.map +1 -1
- package/dist/src/server.d.ts +21 -0
- package/dist/src/{Server.js → server.js} +47 -31
- package/dist/src/server.js.map +1 -0
- package/dist/src/utils.js +4 -4
- package/dist/src/utils.js.map +1 -1
- package/index.ts +3 -3
- package/package.json +59 -59
- package/src/HTTPInterceptor.ts +340 -339
- package/src/Server.ts +178 -167
- package/src/__mocks__/react-native/Libraries/Blob/FileReader.js +44 -44
- package/src/__mocks__/react-native/Libraries/Network/XHRInterceptor.js +39 -39
- package/src/__tests__/HTTPInterceptor.test.ts +322 -322
- package/src/__tests__/Server.test.ts +149 -149
- package/src/__tests__/utils.test.ts +113 -112
- package/src/common.ts +57 -70
- package/src/logPlugin.ts +231 -224
- package/src/logger.ts +14 -14
- package/src/utils.ts +112 -112
- package/tsconfig.json +26 -26
- package/dist/src/Server.d.ts +0 -20
- package/dist/src/Server.js.map +0 -1
package/src/logPlugin.ts
CHANGED
@@ -1,224 +1,231 @@
|
|
1
|
-
import Server from './Server';
|
2
|
-
import { createClassWithErrorHandling } from './utils';
|
3
|
-
import { httpInterceptor } from './HTTPInterceptor';
|
4
|
-
import { DEFAULT_TIMEOUT, getDefaultStorage, Level, LOG_KEY, Tag, URLS_KEY } from './common';
|
5
|
-
import logger from './logger';
|
6
|
-
|
7
|
-
type Options = {
|
8
|
-
/**
|
9
|
-
* storage 用于存储已设置的日志系统的 url
|
10
|
-
* @default @react-native-async-storage/async-storage
|
11
|
-
*/
|
12
|
-
storage?: Storage
|
13
|
-
/**
|
14
|
-
* 设置上传日志的超时时间,单位为毫秒
|
15
|
-
* @default 3000
|
16
|
-
*/
|
17
|
-
timeout?: number
|
18
|
-
/**
|
19
|
-
* 日志系统的url
|
20
|
-
*/
|
21
|
-
testUrl?: string
|
22
|
-
/**
|
23
|
-
* 是否自动开启日志记录
|
24
|
-
* @default false
|
25
|
-
*/
|
26
|
-
isAuto?: boolean
|
27
|
-
/**
|
28
|
-
* 设置日志系统的基础数据,这些数据会自动添加到每条日志中
|
29
|
-
*/
|
30
|
-
baseData?: Record<string, any>
|
31
|
-
}
|
32
|
-
|
33
|
-
class LogPlugin {
|
34
|
-
private server: Server | null = null;
|
35
|
-
private timeout: number | null = null;
|
36
|
-
private isAuto = false
|
37
|
-
private storage: Storage | null = getDefaultStorage();
|
38
|
-
|
39
|
-
constructor() {
|
40
|
-
this.init()
|
41
|
-
}
|
42
|
-
|
43
|
-
private init = async () => {
|
44
|
-
this.server = new Server();
|
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
|
-
this.
|
71
|
-
this.
|
72
|
-
this.
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
this.
|
81
|
-
this.
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
httpInterceptor.
|
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
|
-
headers: data.
|
141
|
-
body: data.
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
this.
|
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
|
-
|
1
|
+
import Server from './Server';
|
2
|
+
import { createClassWithErrorHandling } from './utils';
|
3
|
+
import { httpInterceptor } from './HTTPInterceptor';
|
4
|
+
import { DEFAULT_TIMEOUT, getDefaultStorage, Level, LOG_KEY, Tag, URLS_KEY } from './common';
|
5
|
+
import logger from './logger';
|
6
|
+
|
7
|
+
type Options = {
|
8
|
+
/**
|
9
|
+
* storage 用于存储已设置的日志系统的 url
|
10
|
+
* @default @react-native-async-storage/async-storage
|
11
|
+
*/
|
12
|
+
storage?: Storage
|
13
|
+
/**
|
14
|
+
* 设置上传日志的超时时间,单位为毫秒
|
15
|
+
* @default 3000
|
16
|
+
*/
|
17
|
+
timeout?: number
|
18
|
+
/**
|
19
|
+
* 日志系统的url
|
20
|
+
*/
|
21
|
+
testUrl?: string
|
22
|
+
/**
|
23
|
+
* 是否自动开启日志记录
|
24
|
+
* @default false
|
25
|
+
*/
|
26
|
+
isAuto?: boolean
|
27
|
+
/**
|
28
|
+
* 设置日志系统的基础数据,这些数据会自动添加到每条日志中
|
29
|
+
*/
|
30
|
+
baseData?: Record<string, any>
|
31
|
+
}
|
32
|
+
|
33
|
+
class LogPlugin {
|
34
|
+
private server: Server | null = null;
|
35
|
+
private timeout: number | null = null;
|
36
|
+
private isAuto = false
|
37
|
+
private storage: Storage | null = getDefaultStorage();
|
38
|
+
|
39
|
+
constructor() {
|
40
|
+
this.init()
|
41
|
+
}
|
42
|
+
|
43
|
+
private init = async () => {
|
44
|
+
this.server = new Server();
|
45
|
+
|
46
|
+
if (this.storage) {
|
47
|
+
const urlsStr = await this.storage.getItem(URLS_KEY)
|
48
|
+
if (urlsStr) {
|
49
|
+
const urls = JSON.parse(urlsStr)
|
50
|
+
this.server.setBaseUrlArr(new Set(urls))
|
51
|
+
}
|
52
|
+
}
|
53
|
+
|
54
|
+
|
55
|
+
this.server.addUrlsListener((urlArr) => {
|
56
|
+
if (this.storage) {
|
57
|
+
this.storage.setItem(URLS_KEY, JSON.stringify(urlArr))
|
58
|
+
}
|
59
|
+
httpInterceptor.setIgnoredUrls(this.handleIgnoredUrls())
|
60
|
+
})
|
61
|
+
}
|
62
|
+
|
63
|
+
config = ({ storage, timeout, testUrl, isAuto, baseData = {} }: Options) => {
|
64
|
+
if (isAuto) {
|
65
|
+
this.auto()
|
66
|
+
} else {
|
67
|
+
this.unAuto()
|
68
|
+
}
|
69
|
+
this.storage = storage ?? getDefaultStorage();
|
70
|
+
this.setTimeout(timeout ?? DEFAULT_TIMEOUT)
|
71
|
+
this.setBaseUrl(testUrl)
|
72
|
+
this.setBaseData(baseData)
|
73
|
+
};
|
74
|
+
|
75
|
+
/**
|
76
|
+
* @deprecated 这个方法将在下一个主要版本中被移除。请使用 config({isAuto: true}) 替代。
|
77
|
+
*/
|
78
|
+
auto = () => {
|
79
|
+
this.startRecordNetwork();
|
80
|
+
this.startRecordLog();
|
81
|
+
this.isAuto = true
|
82
|
+
}
|
83
|
+
|
84
|
+
unAuto = () => {
|
85
|
+
this.stopRecordLog()
|
86
|
+
httpInterceptor.disable()
|
87
|
+
httpInterceptor.removeAllListener()
|
88
|
+
this.isAuto = false
|
89
|
+
}
|
90
|
+
|
91
|
+
startRecordLog = () => {
|
92
|
+
console.log = (...data: any[]) => {
|
93
|
+
this.log(...data);
|
94
|
+
if (!__DEV__) {
|
95
|
+
return
|
96
|
+
}
|
97
|
+
logger.log(...data)
|
98
|
+
};
|
99
|
+
|
100
|
+
console.warn = (...data: any[]) => {
|
101
|
+
this.warn(...data);
|
102
|
+
if (!__DEV__) {
|
103
|
+
return
|
104
|
+
}
|
105
|
+
logger.warn(...data)
|
106
|
+
};
|
107
|
+
|
108
|
+
console.error = (...data: any[]) => {
|
109
|
+
this.error(...data);
|
110
|
+
if (!__DEV__) {
|
111
|
+
return
|
112
|
+
}
|
113
|
+
logger.error(...data)
|
114
|
+
};
|
115
|
+
}
|
116
|
+
|
117
|
+
stopRecordLog = () => {
|
118
|
+
console.log = logger.log
|
119
|
+
console.warn = logger.warn
|
120
|
+
console.error = logger.error
|
121
|
+
}
|
122
|
+
|
123
|
+
private handleIgnoredUrls = () => {
|
124
|
+
const urls = this.server?.getBaseUrlArr?.()
|
125
|
+
const ignoredUrls: string[] = []
|
126
|
+
if (urls?.size) {
|
127
|
+
urls.forEach((url) => {
|
128
|
+
ignoredUrls.push(`${url}/log`, `${url}/network`, `${url}/join`)
|
129
|
+
})
|
130
|
+
}
|
131
|
+
return ignoredUrls
|
132
|
+
}
|
133
|
+
|
134
|
+
startRecordNetwork = () => {
|
135
|
+
httpInterceptor.addListener("send", (data) => {
|
136
|
+
this.server?.network({
|
137
|
+
url: data.url,
|
138
|
+
id: data.id,
|
139
|
+
method: data.method,
|
140
|
+
headers: data.requestHeaders,
|
141
|
+
body: data.requestData,
|
142
|
+
createTime: data.startTime
|
143
|
+
})
|
144
|
+
})
|
145
|
+
httpInterceptor.addListener("response", (data) => {
|
146
|
+
this.server?.network({
|
147
|
+
headers: data.responseHeaders,
|
148
|
+
body: data.responseData,
|
149
|
+
requestId: data.id,
|
150
|
+
statusCode: data.status,
|
151
|
+
endTime: data.endTime
|
152
|
+
})
|
153
|
+
})
|
154
|
+
httpInterceptor.enable({ignoredUrls: this.handleIgnoredUrls()})
|
155
|
+
}
|
156
|
+
|
157
|
+
/**
|
158
|
+
* @deprecated 这个方法将在下一个主要版本中被移除。请使用 config({testUrl: ''}) 替代。
|
159
|
+
*/
|
160
|
+
setBaseUrl = (url: string) => {
|
161
|
+
const tempUrl = url?.trim()
|
162
|
+
if (this.server) {
|
163
|
+
this.server.updateUrl(tempUrl);
|
164
|
+
} else {
|
165
|
+
this.server = new Server(tempUrl);
|
166
|
+
}
|
167
|
+
httpInterceptor.setIgnoredUrls(this.handleIgnoredUrls())
|
168
|
+
if (this.isAuto) {
|
169
|
+
this.startRecordNetwork();
|
170
|
+
this.startRecordLog()
|
171
|
+
}
|
172
|
+
}
|
173
|
+
|
174
|
+
/**
|
175
|
+
* @deprecated 这个方法将在下一个主要版本中被移除。请使用 config({timeout: 3000}) 替代。
|
176
|
+
*/
|
177
|
+
setTimeout = (timeout: number) => {
|
178
|
+
if (typeof timeout === 'number') {
|
179
|
+
this.timeout = timeout;
|
180
|
+
this.server?.updateTimeout(this.timeout);
|
181
|
+
}
|
182
|
+
}
|
183
|
+
|
184
|
+
/**
|
185
|
+
* @deprecated 这个方法将在下一个主要版本中被移除。移除后将不再支持获取超时时间。
|
186
|
+
*/
|
187
|
+
getTimeout = () => {
|
188
|
+
if (typeof this.timeout === 'number') {
|
189
|
+
return this.timeout;
|
190
|
+
}
|
191
|
+
return null;
|
192
|
+
}
|
193
|
+
|
194
|
+
/**
|
195
|
+
* @deprecated 这个方法将在下一个主要版本中被移除。请使用 config({baseData: {}}) 替代。
|
196
|
+
*/
|
197
|
+
setBaseData = (data: Record<string, any> = {}) => {
|
198
|
+
this.server.updateBaseData(data)
|
199
|
+
}
|
200
|
+
|
201
|
+
private _log = (level: string, tag: string, ...data: any[]) => {
|
202
|
+
const sendData = {
|
203
|
+
message: data,
|
204
|
+
tag,
|
205
|
+
level: level ?? 'log',
|
206
|
+
createTime: Date.now(),
|
207
|
+
};
|
208
|
+
this.server?.log(sendData);
|
209
|
+
}
|
210
|
+
|
211
|
+
tag = (tag: string, ...data: any[]) => {
|
212
|
+
this._log(Level.LOG, tag, ...data);
|
213
|
+
}
|
214
|
+
|
215
|
+
log = (...data: any[]) => {
|
216
|
+
this._log(Level.LOG, Tag.DEFAULT, ...data);
|
217
|
+
}
|
218
|
+
|
219
|
+
warn = (...data: any[]) => {
|
220
|
+
this._log(Level.WARN, Tag.DEFAULT, ...data);
|
221
|
+
}
|
222
|
+
|
223
|
+
error = (...data: any[]) => {
|
224
|
+
this._log(Level.ERROR, Tag.DEFAULT, ...data);
|
225
|
+
}
|
226
|
+
|
227
|
+
}
|
228
|
+
const SafeLogPlugin = createClassWithErrorHandling(LogPlugin)
|
229
|
+
const logPlugin = new SafeLogPlugin();
|
230
|
+
export { SafeLogPlugin };
|
231
|
+
export default logPlugin;
|
package/src/logger.ts
CHANGED
@@ -1,15 +1,15 @@
|
|
1
|
-
const [log, warn, error] = [console.log, console.warn, console.error];
|
2
|
-
|
3
|
-
const logger = {
|
4
|
-
log: (...data: any[]) => {
|
5
|
-
log(...data)
|
6
|
-
},
|
7
|
-
warn: (...data: any[]) => {
|
8
|
-
warn(...data)
|
9
|
-
},
|
10
|
-
error: (...data: any[]) => {
|
11
|
-
error(...data)
|
12
|
-
},
|
13
|
-
}
|
14
|
-
|
1
|
+
const [log, warn, error] = [console.log, console.warn, console.error];
|
2
|
+
|
3
|
+
const logger = {
|
4
|
+
log: (...data: any[]) => {
|
5
|
+
log(...data)
|
6
|
+
},
|
7
|
+
warn: (...data: any[]) => {
|
8
|
+
warn(...data)
|
9
|
+
},
|
10
|
+
error: (...data: any[]) => {
|
11
|
+
error(...data)
|
12
|
+
},
|
13
|
+
}
|
14
|
+
|
15
15
|
export default logger
|