@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.
Files changed (37) hide show
  1. package/LICENSE +201 -201
  2. package/README.md +163 -163
  3. package/dist/src/HTTPInterceptor.js +2 -1
  4. package/dist/src/HTTPInterceptor.js.map +1 -1
  5. package/dist/src/__tests__/Server.test.js +4 -4
  6. package/dist/src/__tests__/Server.test.js.map +1 -1
  7. package/dist/src/__tests__/console.test.d.ts +1 -0
  8. package/dist/src/__tests__/console.test.js +29 -0
  9. package/dist/src/__tests__/console.test.js.map +1 -0
  10. package/dist/src/__tests__/utils.test.js +5 -1
  11. package/dist/src/__tests__/utils.test.js.map +1 -1
  12. package/dist/src/common.d.ts +1 -7
  13. package/dist/src/common.js +2 -14
  14. package/dist/src/common.js.map +1 -1
  15. package/dist/src/logPlugin.js +22 -17
  16. package/dist/src/logPlugin.js.map +1 -1
  17. package/dist/src/server.d.ts +21 -0
  18. package/dist/src/{Server.js → server.js} +47 -31
  19. package/dist/src/server.js.map +1 -0
  20. package/dist/src/utils.js +4 -4
  21. package/dist/src/utils.js.map +1 -1
  22. package/index.ts +3 -3
  23. package/package.json +59 -59
  24. package/src/HTTPInterceptor.ts +340 -339
  25. package/src/Server.ts +178 -167
  26. package/src/__mocks__/react-native/Libraries/Blob/FileReader.js +44 -44
  27. package/src/__mocks__/react-native/Libraries/Network/XHRInterceptor.js +39 -39
  28. package/src/__tests__/HTTPInterceptor.test.ts +322 -322
  29. package/src/__tests__/Server.test.ts +149 -149
  30. package/src/__tests__/utils.test.ts +113 -112
  31. package/src/common.ts +57 -70
  32. package/src/logPlugin.ts +231 -224
  33. package/src/logger.ts +14 -14
  34. package/src/utils.ts +112 -112
  35. package/tsconfig.json +26 -26
  36. package/dist/src/Server.d.ts +0 -20
  37. 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
- if (!this.storage) {
46
- logger.warn(LOG_KEY, '你并没有设置 storage ,这会导致 App 杀死后可能需要重新加入日志系统才能收集日志数据,建议你设置 storage 。')
47
- } else {
48
- const urlsStr = await this.storage.getItem(URLS_KEY)
49
- if (urlsStr) {
50
- const urls = JSON.parse(urlsStr)
51
- this.server.setBaseUrlObj(urls)
52
- }
53
- }
54
-
55
-
56
- this.server.addUrlsListener((_, urlsObj) => {
57
- if (this.storage) {
58
- this.storage.setItem(URLS_KEY, JSON.stringify(urlsObj))
59
- }
60
- httpInterceptor.setIgnoredUrls(this.handleIgnoredUrls())
61
- })
62
- }
63
-
64
- config = ({ storage, timeout, testUrl, isAuto, baseData = {} }: Options) => {
65
- if (isAuto) {
66
- this.auto()
67
- } else {
68
- this.unAuto()
69
- }
70
- this.storage = storage ?? getDefaultStorage();
71
- this.setTimeout(timeout ?? DEFAULT_TIMEOUT)
72
- this.setBaseUrl(testUrl)
73
- this.setBaseData(baseData)
74
- };
75
-
76
- /**
77
- * @deprecated 这个方法将在下一个主要版本中被移除。请使用 config({isAuto: true}) 替代。
78
- */
79
- auto = () => {
80
- this.startRecordNetwork();
81
- this.startRecordLog();
82
- this.isAuto = true
83
- }
84
-
85
- unAuto = () => {
86
- this.stopRecordLog()
87
- httpInterceptor.disable()
88
- httpInterceptor.removeAllListener()
89
- this.isAuto = false
90
- }
91
-
92
- startRecordLog = () => {
93
- console.log = (...data: any[]) => {
94
- logger.log(...data)
95
- this.log(...data);
96
- };
97
-
98
- console.warn = (...data: any[]) => {
99
- logger.warn(...data)
100
- this.warn(...data);
101
- };
102
-
103
- console.error = (...data: any[]) => {
104
- logger.error(...data)
105
- this.error(...data);
106
- };
107
- }
108
-
109
- stopRecordLog = () => {
110
- console.log = logger.log
111
- console.warn = logger.warn
112
- console.error = logger.error
113
- }
114
-
115
- private handleIgnoredUrls = () => {
116
- const urls = this.server?.getUrls?.()
117
- let ignoredUrls: string[] = []
118
- if (urls?.length) {
119
- ignoredUrls = urls.reduce((acc, url) => {
120
- acc.push(`${url}/log`, `${url}/network`, `${url}/join`)
121
- return acc
122
- }, [] as string[])
123
- }
124
- return ignoredUrls
125
- }
126
-
127
- startRecordNetwork = () => {
128
- httpInterceptor.addListener("send", (data) => {
129
- this.server?.network({
130
- url: data.url,
131
- id: data.id,
132
- method: data.method,
133
- headers: data.requestHeaders,
134
- body: data.requestData,
135
- createTime: data.startTime
136
- })
137
- })
138
- httpInterceptor.addListener("response", (data) => {
139
- this.server?.network({
140
- headers: data.responseHeaders,
141
- body: data.responseData,
142
- requestId: data.id,
143
- statusCode: data.status,
144
- endTime: data.endTime
145
- })
146
- })
147
- httpInterceptor.enable({ignoredUrls: this.handleIgnoredUrls()})
148
- }
149
-
150
- /**
151
- * @deprecated 这个方法将在下一个主要版本中被移除。请使用 config({testUrl: ''}) 替代。
152
- */
153
- setBaseUrl = (url: string) => {
154
- const tempUrl = url?.trim()
155
- if (this.server) {
156
- this.server.updateUrl(tempUrl);
157
- } else {
158
- this.server = new Server(tempUrl);
159
- }
160
- httpInterceptor.setIgnoredUrls(this.handleIgnoredUrls())
161
- if (this.isAuto) {
162
- this.startRecordNetwork();
163
- this.startRecordLog()
164
- }
165
- }
166
-
167
- /**
168
- * @deprecated 这个方法将在下一个主要版本中被移除。请使用 config({timeout: 3000}) 替代。
169
- */
170
- setTimeout = (timeout: number) => {
171
- if (typeof timeout === 'number') {
172
- this.timeout = timeout;
173
- this.server?.updateTimeout(this.timeout);
174
- }
175
- }
176
-
177
- /**
178
- * @deprecated 这个方法将在下一个主要版本中被移除。移除后将不再支持获取超时时间。
179
- */
180
- getTimeout = () => {
181
- if (typeof this.timeout === 'number') {
182
- return this.timeout;
183
- }
184
- return null;
185
- }
186
-
187
- /**
188
- * @deprecated 这个方法将在下一个主要版本中被移除。请使用 config({baseData: {}}) 替代。
189
- */
190
- setBaseData = (data: Record<string, any> = {}) => {
191
- this.server.updateBaseData(data)
192
- }
193
-
194
- private _log = (level: string, tag: string, ...data: any[]) => {
195
- const sendData = {
196
- message: data,
197
- tag,
198
- level: level ?? 'log',
199
- createTime: Date.now(),
200
- };
201
- this.server?.log(sendData);
202
- }
203
-
204
- tag = (tag: string, ...data: any[]) => {
205
- this._log(Level.LOG, tag, ...data);
206
- }
207
-
208
- log = (...data: any[]) => {
209
- this._log(Level.LOG, Tag.DEFAULT, ...data);
210
- }
211
-
212
- warn = (...data: any[]) => {
213
- this._log(Level.WARN, Tag.DEFAULT, ...data);
214
- }
215
-
216
- error = (...data: any[]) => {
217
- this._log(Level.ERROR, Tag.DEFAULT, ...data);
218
- }
219
-
220
- }
221
- const SafeLogPlugin = createClassWithErrorHandling(LogPlugin)
222
- const logPlugin = new SafeLogPlugin();
223
- export { SafeLogPlugin };
224
- export default logPlugin;
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