@pluve/logger-sdk 0.0.13 → 0.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
|
@@ -12,33 +12,26 @@
|
|
|
12
12
|
## 快速开始
|
|
13
13
|
|
|
14
14
|
```ts
|
|
15
|
-
import { LoggerSDK } from '
|
|
15
|
+
import { LoggerSDK } from '@pluve/logger-sdk';
|
|
16
16
|
|
|
17
17
|
const sdk = LoggerSDK.getInstance();
|
|
18
18
|
|
|
19
19
|
sdk.init({
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
logStage: 'develop',
|
|
24
|
-
debug: true,
|
|
25
|
-
// 采集与性能
|
|
20
|
+
appId: 'app',
|
|
21
|
+
logStage: 'product',
|
|
22
|
+
debug: false,
|
|
26
23
|
enableGzip: true,
|
|
27
|
-
enableBatch:
|
|
28
|
-
batchSize:
|
|
24
|
+
enableBatch: false,
|
|
25
|
+
batchSize: 20,
|
|
29
26
|
batchInterval: 15000,
|
|
30
27
|
maxPixelUrlLen: 8192,
|
|
31
|
-
|
|
32
|
-
enableRetry: true,
|
|
33
|
-
maxRetries: 3,
|
|
34
|
-
retryDelay: 1000,
|
|
35
|
-
retryBackoff: true,
|
|
36
|
-
// 身份与鉴权
|
|
28
|
+
enableRetry: false,
|
|
37
29
|
userId: 'u-001',
|
|
38
30
|
storeCode: 's-001',
|
|
39
31
|
token: '<token>',
|
|
40
|
-
// 采样率(可选)
|
|
41
32
|
sampleRate: 1,
|
|
33
|
+
enableAutoCapture: true,
|
|
34
|
+
autoCapture: { js: true, promise: true, resource: true, wechat: true },
|
|
42
35
|
});
|
|
43
36
|
|
|
44
37
|
// 业务日志
|
|
@@ -64,16 +57,18 @@ sdk.setStage('testing');
|
|
|
64
57
|
- enableStorage:队列持久化开关(默认 true)
|
|
65
58
|
- enableRetry/maxRetries/retryDelay/retryBackoff:重试相关配置
|
|
66
59
|
- sampleRate/levelSampleRate/sourceSampleRate/pathSampleRate:采样配置
|
|
60
|
+
- enableAutoCapture:自动采集总开关(默认开启)
|
|
61
|
+
- autoCapture:细粒度开关 { js, promise, resource, wechat }
|
|
67
62
|
|
|
68
|
-
详细定义参考类型文件:[sdkOptions.ts](src/
|
|
63
|
+
详细定义参考类型文件:[sdkOptions.ts](./src/types/sdkOptions.ts).
|
|
69
64
|
|
|
70
65
|
## 压缩实现
|
|
71
66
|
|
|
72
67
|
- gzipCompress(data)
|
|
73
68
|
- 浏览器支持 CompressionStream 时,流式 gzip 压缩,再转 Base64 字符串
|
|
74
|
-
- 不支持时,回退到 fflate.gzipSync
|
|
69
|
+
- 不支持时,回退到 fflate.gzipSync,使用 btoa/Buffer 生成 Base64
|
|
75
70
|
- 返回值为 Base64 字符串,适用于传输(像素图 URL/JSON 体)
|
|
76
|
-
- 代码参考:[compression.ts](src/
|
|
71
|
+
- 代码参考:[compression.ts](./src/compress/compression.ts).
|
|
77
72
|
- 备注:当前默认压缩级别,无级别可调参数暴露;如需级别控制可在 fflate 路径引入配置。
|
|
78
73
|
|
|
79
74
|
## 传输适配
|
|
@@ -85,7 +80,7 @@ sdk.setStage('testing');
|
|
|
85
80
|
- 请求体字符串化 safeStringify
|
|
86
81
|
- enableGzip 时使用 gzipCompress 生成 Base64,Content-Type 设为 `application/json; charset=utf-8`
|
|
87
82
|
- PixelImageTransport
|
|
88
|
-
- 字符串化 items,enableGzip 时压缩为 Base64
|
|
83
|
+
- 字符串化 items,enableGzip 时压缩为 Base64;非 gzip 情况下优先使用全局 Base64,其次 btoa,再次 encodeURIComponent;URL 参数附带 `gzip=1`
|
|
89
84
|
- 构造像素上报 URL(含 appId、appStage、items、cacheBuster)
|
|
90
85
|
- URL 超长打印警告
|
|
91
86
|
- BeaconTransport
|
|
@@ -93,26 +88,26 @@ sdk.setStage('testing');
|
|
|
93
88
|
|
|
94
89
|
代码参考:
|
|
95
90
|
|
|
96
|
-
- 适配器选择:[transportAdapter.ts](src/
|
|
97
|
-
- WeChat:[wechatTransport.ts](src/
|
|
98
|
-
- Pixel Image:[pixelImageTransport.ts](src/
|
|
99
|
-
- Beacon:[beaconTransport.ts](src/
|
|
91
|
+
- 适配器选择:[transportAdapter.ts](./src/transport/transportAdapter.ts)
|
|
92
|
+
- WeChat:[wechatTransport.ts](./src/transport/wechatTransport.ts)
|
|
93
|
+
- Pixel Image:[pixelImageTransport.ts](./src/transport/pixelImageTransport.ts)
|
|
94
|
+
- Beacon:[beaconTransport.ts](./src/transport/beaconTransport.ts)
|
|
100
95
|
|
|
101
96
|
## 批量与队列
|
|
102
97
|
|
|
103
98
|
- 启用批量后,事件入队,当队列大小达到 batchSize 或定时器触发时 flush
|
|
104
99
|
- flush:按 appId|stage 分组与分块(最多 200/块),批量发送成功后出队
|
|
105
|
-
-
|
|
100
|
+
- 队列持久化:优先 IndexedDB(localforage),失败回退 localStorage;仅在浏览器环境启用
|
|
106
101
|
- 页面事件触发 flush:visibilitychange(hidden)、pagehide、beforeunload
|
|
107
102
|
- 代码参考:
|
|
108
|
-
- [queueManager.ts](src/
|
|
109
|
-
- [loggerSDK.ts(flush/sendBatch)](src/
|
|
103
|
+
- [queueManager.ts](./src/core/queueManager.ts)
|
|
104
|
+
- [loggerSDK.ts(flush/sendBatch)](./src/core/loggerSDK.ts#L394-L467)
|
|
110
105
|
|
|
111
106
|
## 重试策略
|
|
112
107
|
|
|
113
108
|
- 单个事件 sendEvent 与批量 sendBatch 均支持 executeWithRetry
|
|
114
109
|
- 可配置最大次数、基础延迟、指数退避
|
|
115
|
-
- 代码参考:[retryManager.ts](src/
|
|
110
|
+
- 代码参考:[retryManager.ts](./src/core/retryManager.ts)
|
|
116
111
|
|
|
117
112
|
## 采样与开关
|
|
118
113
|
|
|
@@ -124,13 +119,13 @@ sdk.setStage('testing');
|
|
|
124
119
|
- 综合 level/source/path/global 采样率
|
|
125
120
|
- 基于 seed 哈希概率决定是否发送
|
|
126
121
|
- 参考:
|
|
127
|
-
- [loggerSDK.ts(shouldSend)](src/
|
|
128
|
-
- [api.ts](src/
|
|
122
|
+
- [loggerSDK.ts(shouldSend)](./src/core/loggerSDK.ts#L324-L365)
|
|
123
|
+
- [api.ts](./src/types/api.ts)
|
|
129
124
|
|
|
130
125
|
## 数据格式
|
|
131
126
|
|
|
132
127
|
- LogEvent:标准化日志格式(logId/seq/appId/stage/level/traceId/frontendId/url/location/message/throwable/userId/storeCode/tags)
|
|
133
|
-
- 类型参考:[logEvent.ts](src/
|
|
128
|
+
- 类型参考:[logEvent.ts](./src/types/logEvent.ts)
|
|
134
129
|
- ReportData:上报批次(appId/appStage/items[])
|
|
135
130
|
|
|
136
131
|
## 限制与注意
|
|
@@ -139,8 +134,16 @@ sdk.setStage('testing');
|
|
|
139
134
|
- 像素图:URL 长度受限;压缩可显著降低长度但 Base64 会比二进制略增
|
|
140
135
|
- 压缩:CompressionStream 不提供压缩级别参数;fflate 同步压缩适合小批量数据
|
|
141
136
|
- 安全:不要在日志中包含敏感数据(token、密码、隐私信息等)
|
|
137
|
+
- 依赖:js-base64、localforage 均为 peerDependencies;UMD 外部化由应用或 CDN 提供全局;ESM/CJS 下由应用安装解析
|
|
142
138
|
|
|
143
139
|
## 流程图
|
|
144
140
|
|
|
145
141
|
- 完整 Mermaid 流程图见文件:
|
|
146
|
-
- [logger-sdk.mermaid](src/
|
|
142
|
+
- [logger-sdk.mermaid](./src/logger-sdk.mermaid)
|
|
143
|
+
- [logger-sdk.svg](./src/logger-sdk.svg)
|
|
144
|
+
|
|
145
|
+
## 类型与打包
|
|
146
|
+
|
|
147
|
+
- 类型入口:package.json exports.types 与 typesVersions 指向 dist/types
|
|
148
|
+
- TypeScript 解析:推荐在应用设置 moduleResolution: "bundler" 或 "nodenext"/"node16"
|
|
149
|
+
- 声明构建:`yarn build-types`(tsconfig.types.json 禁用默认 @types 扫描,避免冲突)
|
|
@@ -40,7 +40,7 @@ async function gzipCompress(data) {
|
|
|
40
40
|
} catch (e) {
|
|
41
41
|
console.log("gzipCompress 压缩失败", e);
|
|
42
42
|
}
|
|
43
|
-
|
|
43
|
+
throw new Error("gzipCompress 不支持压缩");
|
|
44
44
|
}
|
|
45
45
|
function convert2Base64FromArray(data) {
|
|
46
46
|
let binary = "";
|
|
@@ -55,7 +55,7 @@ function convert2Base64FromArray(data) {
|
|
|
55
55
|
}
|
|
56
56
|
throw new Error("convert2Base64FromArray 不支持转换");
|
|
57
57
|
}
|
|
58
|
-
function convert2Base64(data) {
|
|
58
|
+
async function convert2Base64(data) {
|
|
59
59
|
if (typeof btoa !== "undefined") {
|
|
60
60
|
try {
|
|
61
61
|
return btoa(data);
|
|
@@ -65,6 +65,12 @@ function convert2Base64(data) {
|
|
|
65
65
|
if (typeof Buffer !== "undefined") {
|
|
66
66
|
return Buffer.from(data, "base64").toString("base64");
|
|
67
67
|
}
|
|
68
|
+
try {
|
|
69
|
+
const mod = await import("js-base64");
|
|
70
|
+
return mod.Base64.encode(data);
|
|
71
|
+
} catch (e) {
|
|
72
|
+
console.log("convert2Base64 动态引入 js-base64 库失败", e);
|
|
73
|
+
}
|
|
68
74
|
return data;
|
|
69
75
|
}
|
|
70
76
|
function isGzipSupported() {
|
|
@@ -23,10 +23,7 @@ var QueueManager = class {
|
|
|
23
23
|
return this.localforage;
|
|
24
24
|
}
|
|
25
25
|
try {
|
|
26
|
-
const mod = await import(
|
|
27
|
-
/* @vite-ignore */
|
|
28
|
-
"localforage"
|
|
29
|
-
);
|
|
26
|
+
const mod = await import("localforage");
|
|
30
27
|
this.localforage = mod;
|
|
31
28
|
return this.localforage;
|
|
32
29
|
} catch (e) {
|
|
@@ -24,11 +24,12 @@ var PixelImageTransport = class {
|
|
|
24
24
|
const t = now();
|
|
25
25
|
logDebug(!!((_e = this.opts) == null ? void 0 : _e.debug), "PixelImage request gzip compress body: ", body);
|
|
26
26
|
compressedBody = await gzipCompress(body);
|
|
27
|
+
compressedBody = await convert2Base64(compressedBody);
|
|
27
28
|
logDebug(!!((_f = this.opts) == null ? void 0 : _f.debug), "PixelImage request gzip compress cost: ", now() - t);
|
|
28
29
|
logDebug(!!((_g = this.opts) == null ? void 0 : _g.debug), `original body size: ${body.length}, compressed body size: ${compressedBody.length}`);
|
|
29
30
|
logDebug(!!((_h = this.opts) == null ? void 0 : _h.debug), "PixelImage request gzip compress body: ", compressedBody);
|
|
30
31
|
} else {
|
|
31
|
-
compressedBody = convert2Base64(body);
|
|
32
|
+
compressedBody = await convert2Base64(body);
|
|
32
33
|
}
|
|
33
34
|
const cacheBuster = `_=${Date.now()}`;
|
|
34
35
|
const qs = `appId=${((_i = this.opts) == null ? void 0 : _i.appId) || ""}&appStage=${((_j = this.opts) == null ? void 0 : _j.logStage) || ""}&${param}=${encodeURIComponent(compressedBody)}&gzip=${((_k = this.opts) == null ? void 0 : _k.enableGzip) ? 1 : 0}&${cacheBuster}`;
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
export declare function gzipCompress(data: string): Promise<string>;
|
|
7
7
|
export declare function convert2Base64FromArray(data: any): any;
|
|
8
|
-
export declare function convert2Base64(data: string): string
|
|
8
|
+
export declare function convert2Base64(data: string): Promise<string>;
|
|
9
9
|
/**
|
|
10
10
|
* 检查是否支持 gzip 压缩
|
|
11
11
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pluve/logger-sdk",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.14",
|
|
4
4
|
"description": "logger sdk",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"logger"
|
|
@@ -59,7 +59,6 @@
|
|
|
59
59
|
"devDependencies": {
|
|
60
60
|
"jsdom": "^27.4.0",
|
|
61
61
|
"vitest": "^4.0.15",
|
|
62
|
-
"localforage": "^1.10.0",
|
|
63
62
|
"@mermaid-js/mermaid-cli": "^10.9.1"
|
|
64
63
|
},
|
|
65
64
|
"dependencies": {
|
|
@@ -67,6 +66,7 @@
|
|
|
67
66
|
"stacktrace-js": "^2.0.2"
|
|
68
67
|
},
|
|
69
68
|
"peerDependencies": {
|
|
70
|
-
"localforage": "^1.10.0"
|
|
69
|
+
"localforage": "^1.10.0",
|
|
70
|
+
"js-base64": "^3.7.8"
|
|
71
71
|
}
|
|
72
72
|
}
|