@wutiange/log-listener-plugin 2.0.1-alpha.2 → 2.0.1-alpha.4

Sign up to get free protection for your applications and to get access to all the features.
package/src/common.ts CHANGED
@@ -1,70 +1,70 @@
1
- import logger from "./logger";
2
-
3
- export const URLS_KEY = 'log-listener-plugin-urls$$key'
4
- export const DEFAULT_TIMEOUT = 3000
5
- export const LOG_KEY = '[@wutiange/log-listener-plugin 日志]'
6
- export enum Level {
7
- LOG = 'log',
8
- WARN = 'warn',
9
- ERROR = 'error',
10
- }
11
-
12
- export enum Tag {
13
- LOG_PLUGIN_INTERNAL_ERROR = 'log-plugin-internal-error',
14
- DEFAULT = 'default',
15
- }
16
-
17
- const getDefaultDeviceINfo = () => {
18
- try {
19
- const {Platform} = require('react-native')
20
- return {
21
- SystemName: Platform.OS,
22
- Version: Platform.Version,
23
- ...Platform.constants
24
- }
25
- } catch (error) {
26
- logger.warn(LOG_KEY, '这个插件只能在 react-native 中使用')
27
- return {}
28
- }
29
- }
30
-
31
- export const getBaseData = (): Record<string, string> => {
32
-
33
- try {
34
- const DeviceInfo = require("react-native-device-info")?.default;
35
- return {
36
- Brand: DeviceInfo.getBrand(),
37
- Model: DeviceInfo.getModel(),
38
- AppVersion: DeviceInfo.getVersion(),
39
- Carrier: DeviceInfo.getCarrierSync(),
40
- Manufacturer: DeviceInfo.getManufacturerSync(),
41
- SystemName: DeviceInfo.getSystemName(),
42
- ...getDefaultDeviceINfo()
43
- };
44
- } catch (error) {
45
- return getDefaultDeviceINfo()
46
- }
47
- }
48
-
49
- export const getDefaultStorage = (): Storage => {
50
- try {
51
- const AsyncStorage = require("@react-native-async-storage/async-storage")?.default;
52
- return AsyncStorage;
53
- } catch (error) {
54
- return null;
55
- }
56
- }
57
-
58
-
59
- export const getErrMsg = (error: any) => {
60
- return {
61
- message: [
62
- `${
63
- error?.message ?? error
64
- }--->这是@wutiange/log-listener-plugin内部错误,请提issue反馈,issue地址:https://github.com/wutiange/log-listener-plugin/issues`,
65
- ],
66
- tag: Tag.LOG_PLUGIN_INTERNAL_ERROR,
67
- level: Level.ERROR,
68
- createTime: Date.now(),
69
- }
1
+ import logger from "./logger";
2
+
3
+ export const URLS_KEY = 'log-listener-plugin-urls$$SetKey'
4
+ export const DEFAULT_TIMEOUT = 3000
5
+ export const LOG_KEY = '[@wutiange/log-listener-plugin 日志]'
6
+ export enum Level {
7
+ LOG = 'log',
8
+ WARN = 'warn',
9
+ ERROR = 'error',
10
+ }
11
+
12
+ export enum Tag {
13
+ LOG_PLUGIN_INTERNAL_ERROR = 'log-plugin-internal-error',
14
+ DEFAULT = 'default',
15
+ }
16
+
17
+ const getDefaultDeviceINfo = () => {
18
+ try {
19
+ const {Platform} = require('react-native')
20
+ return {
21
+ SystemName: Platform.OS,
22
+ Version: Platform.Version,
23
+ ...Platform.constants
24
+ }
25
+ } catch (error) {
26
+ logger.warn(LOG_KEY, '这个插件只能在 react-native 中使用')
27
+ return {}
28
+ }
29
+ }
30
+
31
+ export const getBaseData = (): Record<string, string> => {
32
+
33
+ try {
34
+ const DeviceInfo = require("react-native-device-info")?.default;
35
+ return {
36
+ Brand: DeviceInfo.getBrand(),
37
+ Model: DeviceInfo.getModel(),
38
+ AppVersion: DeviceInfo.getVersion(),
39
+ Carrier: DeviceInfo.getCarrierSync(),
40
+ Manufacturer: DeviceInfo.getManufacturerSync(),
41
+ SystemName: DeviceInfo.getSystemName(),
42
+ ...getDefaultDeviceINfo()
43
+ };
44
+ } catch (error) {
45
+ return getDefaultDeviceINfo()
46
+ }
47
+ }
48
+
49
+ export const getDefaultStorage = (): Storage => {
50
+ try {
51
+ const AsyncStorage = require("@react-native-async-storage/async-storage")?.default;
52
+ return AsyncStorage;
53
+ } catch (error) {
54
+ return null;
55
+ }
56
+ }
57
+
58
+
59
+ export const getErrMsg = (error: any) => {
60
+ return {
61
+ message: [
62
+ `${
63
+ error?.message ?? error
64
+ }--->这是@wutiange/log-listener-plugin内部错误,请提issue反馈,issue地址:https://github.com/wutiange/log-listener-plugin/issues`,
65
+ ],
66
+ tag: Tag.LOG_PLUGIN_INTERNAL_ERROR,
67
+ level: Level.ERROR,
68
+ createTime: Date.now(),
69
+ }
70
70
  }
package/src/logPlugin.ts CHANGED
@@ -1,224 +1,223 @@
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
+ 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.setBaseUrlArr(new Set(urls))
52
+ }
53
+ }
54
+
55
+
56
+ this.server.addUrlsListener((urlArr) => {
57
+ if (this.storage) {
58
+ this.storage.setItem(URLS_KEY, JSON.stringify(urlArr))
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?.getBaseUrlArr?.()
117
+ const ignoredUrls: string[] = []
118
+ if (urls?.size) {
119
+ urls.forEach((url) => {
120
+ ignoredUrls.push(`${url}/log`, `${url}/network`, `${url}/join`)
121
+ })
122
+ }
123
+ return ignoredUrls
124
+ }
125
+
126
+ startRecordNetwork = () => {
127
+ httpInterceptor.addListener("send", (data) => {
128
+ this.server?.network({
129
+ url: data.url,
130
+ id: data.id,
131
+ method: data.method,
132
+ headers: data.requestHeaders,
133
+ body: data.requestData,
134
+ createTime: data.startTime
135
+ })
136
+ })
137
+ httpInterceptor.addListener("response", (data) => {
138
+ this.server?.network({
139
+ headers: data.responseHeaders,
140
+ body: data.responseData,
141
+ requestId: data.id,
142
+ statusCode: data.status,
143
+ endTime: data.endTime
144
+ })
145
+ })
146
+ httpInterceptor.enable({ignoredUrls: this.handleIgnoredUrls()})
147
+ }
148
+
149
+ /**
150
+ * @deprecated 这个方法将在下一个主要版本中被移除。请使用 config({testUrl: ''}) 替代。
151
+ */
152
+ setBaseUrl = (url: string) => {
153
+ const tempUrl = url?.trim()
154
+ if (this.server) {
155
+ this.server.updateUrl(tempUrl);
156
+ } else {
157
+ this.server = new Server(tempUrl);
158
+ }
159
+ httpInterceptor.setIgnoredUrls(this.handleIgnoredUrls())
160
+ if (this.isAuto) {
161
+ this.startRecordNetwork();
162
+ this.startRecordLog()
163
+ }
164
+ }
165
+
166
+ /**
167
+ * @deprecated 这个方法将在下一个主要版本中被移除。请使用 config({timeout: 3000}) 替代。
168
+ */
169
+ setTimeout = (timeout: number) => {
170
+ if (typeof timeout === 'number') {
171
+ this.timeout = timeout;
172
+ this.server?.updateTimeout(this.timeout);
173
+ }
174
+ }
175
+
176
+ /**
177
+ * @deprecated 这个方法将在下一个主要版本中被移除。移除后将不再支持获取超时时间。
178
+ */
179
+ getTimeout = () => {
180
+ if (typeof this.timeout === 'number') {
181
+ return this.timeout;
182
+ }
183
+ return null;
184
+ }
185
+
186
+ /**
187
+ * @deprecated 这个方法将在下一个主要版本中被移除。请使用 config({baseData: {}}) 替代。
188
+ */
189
+ setBaseData = (data: Record<string, any> = {}) => {
190
+ this.server.updateBaseData(data)
191
+ }
192
+
193
+ private _log = (level: string, tag: string, ...data: any[]) => {
194
+ const sendData = {
195
+ message: data,
196
+ tag,
197
+ level: level ?? 'log',
198
+ createTime: Date.now(),
199
+ };
200
+ this.server?.log(sendData);
201
+ }
202
+
203
+ tag = (tag: string, ...data: any[]) => {
204
+ this._log(Level.LOG, tag, ...data);
205
+ }
206
+
207
+ log = (...data: any[]) => {
208
+ this._log(Level.LOG, Tag.DEFAULT, ...data);
209
+ }
210
+
211
+ warn = (...data: any[]) => {
212
+ this._log(Level.WARN, Tag.DEFAULT, ...data);
213
+ }
214
+
215
+ error = (...data: any[]) => {
216
+ this._log(Level.ERROR, Tag.DEFAULT, ...data);
217
+ }
218
+
219
+ }
220
+ const SafeLogPlugin = createClassWithErrorHandling(LogPlugin)
221
+ const logPlugin = new SafeLogPlugin();
222
+ export { SafeLogPlugin };
223
+ 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