@aigc-kino/logger-sdk 1.3.0
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 +280 -0
- package/dist/index.cjs +841 -0
- package/dist/index.d.cts +194 -0
- package/dist/index.d.ts +194 -0
- package/dist/index.global.js +824 -0
- package/dist/index.js +799 -0
- package/package.json +34 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SDK 自带的类型与枚举定义,发布为独立 npm 包后无需额外安装其他依赖即可获得完整类型。
|
|
3
|
+
*
|
|
4
|
+
* ⚠️ 维护约定:LogType 枚举的字符串值必须与日志网关 / 服务端的约定保持一致,
|
|
5
|
+
* 修改此处枚举值需同步通知后端,避免日志 type 字段无法被正确归类。
|
|
6
|
+
*/
|
|
7
|
+
/** 日志类型枚举(运行时值,JSON 上报时序列化为对应字符串) */
|
|
8
|
+
declare enum LogType {
|
|
9
|
+
JsError = "js-error",
|
|
10
|
+
PromiseError = "promise-error",
|
|
11
|
+
VueError = "vue-error",
|
|
12
|
+
Network = "network",
|
|
13
|
+
Resource = "resource",
|
|
14
|
+
Performance = "performance",
|
|
15
|
+
Behavior = "behavior",
|
|
16
|
+
Page = "page",
|
|
17
|
+
Business = "business",
|
|
18
|
+
Console = "console",
|
|
19
|
+
Custom = "custom"
|
|
20
|
+
}
|
|
21
|
+
type LogLevel = 'debug' | 'info' | 'warn' | 'error';
|
|
22
|
+
/** 微前端上下文信息(自动采集,标识当前所处的微前端应用) */
|
|
23
|
+
interface MicroFrontendInfo {
|
|
24
|
+
hostApp?: string;
|
|
25
|
+
microApp?: string;
|
|
26
|
+
route?: string;
|
|
27
|
+
container?: string;
|
|
28
|
+
}
|
|
29
|
+
/** 上报到网关的单条日志结构 */
|
|
30
|
+
interface LogEvent extends MicroFrontendInfo {
|
|
31
|
+
projectId: string;
|
|
32
|
+
projectName?: string;
|
|
33
|
+
env: string;
|
|
34
|
+
version: string;
|
|
35
|
+
type: LogType;
|
|
36
|
+
level: LogLevel;
|
|
37
|
+
message: string;
|
|
38
|
+
stack?: string;
|
|
39
|
+
url: string;
|
|
40
|
+
userId?: string;
|
|
41
|
+
sessionId: string;
|
|
42
|
+
traceId: string;
|
|
43
|
+
browser?: string;
|
|
44
|
+
device?: string;
|
|
45
|
+
network?: string;
|
|
46
|
+
timestamp: number;
|
|
47
|
+
extra?: Record<string, unknown>;
|
|
48
|
+
}
|
|
49
|
+
/** 后台「SDK 配置中心」下发的动态配置结构 */
|
|
50
|
+
interface SdkConfig {
|
|
51
|
+
sampleRate: number;
|
|
52
|
+
ignoreErrors: string[];
|
|
53
|
+
uploadInterval: number;
|
|
54
|
+
batchSize: number;
|
|
55
|
+
enablePerformance: boolean;
|
|
56
|
+
enableBehavior: boolean;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** SDK 上报的用户资料补丁,未提供字段由服务端保留,null 用于显式清除。 */
|
|
60
|
+
interface UserProfileInput {
|
|
61
|
+
nickname?: string | null;
|
|
62
|
+
email?: string | null;
|
|
63
|
+
phone?: string | null;
|
|
64
|
+
avatar?: string | null;
|
|
65
|
+
custom?: Record<string, unknown> | null;
|
|
66
|
+
}
|
|
67
|
+
interface LoggerOptions {
|
|
68
|
+
projectId: string;
|
|
69
|
+
token: string;
|
|
70
|
+
serverUrl: string;
|
|
71
|
+
env?: string;
|
|
72
|
+
version?: string;
|
|
73
|
+
userId?: string;
|
|
74
|
+
enableError?: boolean;
|
|
75
|
+
enablePerformance?: boolean;
|
|
76
|
+
enableBehavior?: boolean;
|
|
77
|
+
enableNetwork?: boolean;
|
|
78
|
+
enableConsole?: boolean;
|
|
79
|
+
sampleRate?: number;
|
|
80
|
+
batchSize?: number;
|
|
81
|
+
uploadInterval?: number;
|
|
82
|
+
maxRetry?: number;
|
|
83
|
+
ignoreErrors?: (string | RegExp)[];
|
|
84
|
+
ignoreUrls?: (string | RegExp)[];
|
|
85
|
+
microFrontend?: MicroFrontendInfo;
|
|
86
|
+
remoteConfig?: boolean;
|
|
87
|
+
debug?: boolean;
|
|
88
|
+
beforeSend?: (log: LogEvent) => LogEvent | false;
|
|
89
|
+
}
|
|
90
|
+
type ResolvedOptions = Required<Omit<LoggerOptions, 'userId' | 'microFrontend' | 'beforeSend'>> & {
|
|
91
|
+
userId?: string;
|
|
92
|
+
microFrontend?: MicroFrontendInfo;
|
|
93
|
+
beforeSend?: (log: LogEvent) => LogEvent | false;
|
|
94
|
+
};
|
|
95
|
+
interface RawLog {
|
|
96
|
+
type: LogType;
|
|
97
|
+
level: LogLevel;
|
|
98
|
+
message: string;
|
|
99
|
+
stack?: string;
|
|
100
|
+
extra?: Record<string, unknown>;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
declare class Logger {
|
|
104
|
+
readonly options: ResolvedOptions;
|
|
105
|
+
readonly sessionId: string;
|
|
106
|
+
readonly traceId: string;
|
|
107
|
+
private transport;
|
|
108
|
+
private tags;
|
|
109
|
+
private cleanups;
|
|
110
|
+
private destroyed;
|
|
111
|
+
constructor(options: LoggerOptions);
|
|
112
|
+
isUploadUrl(url: string): boolean;
|
|
113
|
+
shouldIgnoreUrl(url: string): boolean;
|
|
114
|
+
setUser(userId: string, profile?: UserProfileInput): void;
|
|
115
|
+
/** 退出登录时调用,解除当前用户关联,后续日志不再携带 userId */
|
|
116
|
+
clearUser(): void;
|
|
117
|
+
setTag(key: string, value: unknown): void;
|
|
118
|
+
report(raw: RawLog): void;
|
|
119
|
+
track(event: string, data?: Record<string, unknown>): void;
|
|
120
|
+
captureError(error: unknown, extra?: Record<string, unknown>): void;
|
|
121
|
+
captureMessage(message: string, level?: 'info' | 'warn' | 'error'): void;
|
|
122
|
+
flush(): Promise<void>;
|
|
123
|
+
addCleanup(fn: () => void): void;
|
|
124
|
+
destroy(): void;
|
|
125
|
+
private resolveSessionId;
|
|
126
|
+
private resolveTraceId;
|
|
127
|
+
private pullRemoteConfig;
|
|
128
|
+
/** 上报用户资料;与日志队列隔离,避免资料重试或持久化到 IndexedDB。 */
|
|
129
|
+
private uploadUserProfile;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
interface AxiosLike {
|
|
133
|
+
interceptors: {
|
|
134
|
+
request: {
|
|
135
|
+
use: (fn: (c: AxiosConfigLike) => AxiosConfigLike) => void;
|
|
136
|
+
};
|
|
137
|
+
response: {
|
|
138
|
+
use: (onOk: (r: AxiosResponseLike) => AxiosResponseLike, onErr: (e: AxiosErrorLike) => Promise<never>) => void;
|
|
139
|
+
};
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
interface AxiosConfigLike {
|
|
143
|
+
url?: string;
|
|
144
|
+
method?: string;
|
|
145
|
+
metadata?: {
|
|
146
|
+
start: number;
|
|
147
|
+
requestBody?: string;
|
|
148
|
+
};
|
|
149
|
+
data?: unknown;
|
|
150
|
+
}
|
|
151
|
+
interface AxiosResponseLike {
|
|
152
|
+
status: number;
|
|
153
|
+
config: AxiosConfigLike;
|
|
154
|
+
}
|
|
155
|
+
interface AxiosErrorLike {
|
|
156
|
+
message: string;
|
|
157
|
+
config?: AxiosConfigLike;
|
|
158
|
+
response?: {
|
|
159
|
+
status: number;
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
declare function attachAxiosTo(logger: Logger, axios: AxiosLike): void;
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* 页面地址脱敏:仅掩码敏感查询参数的值,保留非敏感 query 与 hash 路由信息。
|
|
166
|
+
*
|
|
167
|
+
* 为什么不全量剥离 query/hash:业务页面的路由参数(如详情页 id、tab)是排查问题的
|
|
168
|
+
* 关键上下文,全量剥离会导致日志中的页面地址不完整、无法定位页面。
|
|
169
|
+
* 兼容 hash 内携带查询参数的路由形式(如 #/detail?token=xx)。
|
|
170
|
+
*
|
|
171
|
+
* @param url 待脱敏的页面地址(支持相对地址)
|
|
172
|
+
* @returns 脱敏后的地址;解析失败时原样返回,保证不影响上报
|
|
173
|
+
*/
|
|
174
|
+
declare function scrubUrl(url: string): string;
|
|
175
|
+
|
|
176
|
+
declare function init(options: LoggerOptions): Logger;
|
|
177
|
+
declare function setUser(userId: string, profile?: UserProfileInput): void;
|
|
178
|
+
/** 退出登录时调用,解除当前用户关联 */
|
|
179
|
+
declare function clearUser(): void;
|
|
180
|
+
declare function setTag(key: string, value: unknown): void;
|
|
181
|
+
declare function track(event: string, data?: Record<string, unknown>): void;
|
|
182
|
+
declare function captureError(error: unknown, extra?: Record<string, unknown>): void;
|
|
183
|
+
declare function captureMessage(message: string, level?: 'info' | 'warn' | 'error'): void;
|
|
184
|
+
declare function getSessionId(): string;
|
|
185
|
+
declare function getTraceId(): string;
|
|
186
|
+
declare function flush(): Promise<void>;
|
|
187
|
+
declare function destroy(): void;
|
|
188
|
+
declare function attachAxios(axios: Parameters<typeof attachAxiosTo>[1]): void;
|
|
189
|
+
|
|
190
|
+
declare const VuePlugin: {
|
|
191
|
+
install(app: any): void;
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
export { type LogEvent, LogType, Logger, type LoggerOptions, type SdkConfig, type UserProfileInput, VuePlugin, attachAxios, captureError, captureMessage, clearUser, destroy, flush, getSessionId, getTraceId, init, scrubUrl, setTag, setUser, track };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SDK 自带的类型与枚举定义,发布为独立 npm 包后无需额外安装其他依赖即可获得完整类型。
|
|
3
|
+
*
|
|
4
|
+
* ⚠️ 维护约定:LogType 枚举的字符串值必须与日志网关 / 服务端的约定保持一致,
|
|
5
|
+
* 修改此处枚举值需同步通知后端,避免日志 type 字段无法被正确归类。
|
|
6
|
+
*/
|
|
7
|
+
/** 日志类型枚举(运行时值,JSON 上报时序列化为对应字符串) */
|
|
8
|
+
declare enum LogType {
|
|
9
|
+
JsError = "js-error",
|
|
10
|
+
PromiseError = "promise-error",
|
|
11
|
+
VueError = "vue-error",
|
|
12
|
+
Network = "network",
|
|
13
|
+
Resource = "resource",
|
|
14
|
+
Performance = "performance",
|
|
15
|
+
Behavior = "behavior",
|
|
16
|
+
Page = "page",
|
|
17
|
+
Business = "business",
|
|
18
|
+
Console = "console",
|
|
19
|
+
Custom = "custom"
|
|
20
|
+
}
|
|
21
|
+
type LogLevel = 'debug' | 'info' | 'warn' | 'error';
|
|
22
|
+
/** 微前端上下文信息(自动采集,标识当前所处的微前端应用) */
|
|
23
|
+
interface MicroFrontendInfo {
|
|
24
|
+
hostApp?: string;
|
|
25
|
+
microApp?: string;
|
|
26
|
+
route?: string;
|
|
27
|
+
container?: string;
|
|
28
|
+
}
|
|
29
|
+
/** 上报到网关的单条日志结构 */
|
|
30
|
+
interface LogEvent extends MicroFrontendInfo {
|
|
31
|
+
projectId: string;
|
|
32
|
+
projectName?: string;
|
|
33
|
+
env: string;
|
|
34
|
+
version: string;
|
|
35
|
+
type: LogType;
|
|
36
|
+
level: LogLevel;
|
|
37
|
+
message: string;
|
|
38
|
+
stack?: string;
|
|
39
|
+
url: string;
|
|
40
|
+
userId?: string;
|
|
41
|
+
sessionId: string;
|
|
42
|
+
traceId: string;
|
|
43
|
+
browser?: string;
|
|
44
|
+
device?: string;
|
|
45
|
+
network?: string;
|
|
46
|
+
timestamp: number;
|
|
47
|
+
extra?: Record<string, unknown>;
|
|
48
|
+
}
|
|
49
|
+
/** 后台「SDK 配置中心」下发的动态配置结构 */
|
|
50
|
+
interface SdkConfig {
|
|
51
|
+
sampleRate: number;
|
|
52
|
+
ignoreErrors: string[];
|
|
53
|
+
uploadInterval: number;
|
|
54
|
+
batchSize: number;
|
|
55
|
+
enablePerformance: boolean;
|
|
56
|
+
enableBehavior: boolean;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** SDK 上报的用户资料补丁,未提供字段由服务端保留,null 用于显式清除。 */
|
|
60
|
+
interface UserProfileInput {
|
|
61
|
+
nickname?: string | null;
|
|
62
|
+
email?: string | null;
|
|
63
|
+
phone?: string | null;
|
|
64
|
+
avatar?: string | null;
|
|
65
|
+
custom?: Record<string, unknown> | null;
|
|
66
|
+
}
|
|
67
|
+
interface LoggerOptions {
|
|
68
|
+
projectId: string;
|
|
69
|
+
token: string;
|
|
70
|
+
serverUrl: string;
|
|
71
|
+
env?: string;
|
|
72
|
+
version?: string;
|
|
73
|
+
userId?: string;
|
|
74
|
+
enableError?: boolean;
|
|
75
|
+
enablePerformance?: boolean;
|
|
76
|
+
enableBehavior?: boolean;
|
|
77
|
+
enableNetwork?: boolean;
|
|
78
|
+
enableConsole?: boolean;
|
|
79
|
+
sampleRate?: number;
|
|
80
|
+
batchSize?: number;
|
|
81
|
+
uploadInterval?: number;
|
|
82
|
+
maxRetry?: number;
|
|
83
|
+
ignoreErrors?: (string | RegExp)[];
|
|
84
|
+
ignoreUrls?: (string | RegExp)[];
|
|
85
|
+
microFrontend?: MicroFrontendInfo;
|
|
86
|
+
remoteConfig?: boolean;
|
|
87
|
+
debug?: boolean;
|
|
88
|
+
beforeSend?: (log: LogEvent) => LogEvent | false;
|
|
89
|
+
}
|
|
90
|
+
type ResolvedOptions = Required<Omit<LoggerOptions, 'userId' | 'microFrontend' | 'beforeSend'>> & {
|
|
91
|
+
userId?: string;
|
|
92
|
+
microFrontend?: MicroFrontendInfo;
|
|
93
|
+
beforeSend?: (log: LogEvent) => LogEvent | false;
|
|
94
|
+
};
|
|
95
|
+
interface RawLog {
|
|
96
|
+
type: LogType;
|
|
97
|
+
level: LogLevel;
|
|
98
|
+
message: string;
|
|
99
|
+
stack?: string;
|
|
100
|
+
extra?: Record<string, unknown>;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
declare class Logger {
|
|
104
|
+
readonly options: ResolvedOptions;
|
|
105
|
+
readonly sessionId: string;
|
|
106
|
+
readonly traceId: string;
|
|
107
|
+
private transport;
|
|
108
|
+
private tags;
|
|
109
|
+
private cleanups;
|
|
110
|
+
private destroyed;
|
|
111
|
+
constructor(options: LoggerOptions);
|
|
112
|
+
isUploadUrl(url: string): boolean;
|
|
113
|
+
shouldIgnoreUrl(url: string): boolean;
|
|
114
|
+
setUser(userId: string, profile?: UserProfileInput): void;
|
|
115
|
+
/** 退出登录时调用,解除当前用户关联,后续日志不再携带 userId */
|
|
116
|
+
clearUser(): void;
|
|
117
|
+
setTag(key: string, value: unknown): void;
|
|
118
|
+
report(raw: RawLog): void;
|
|
119
|
+
track(event: string, data?: Record<string, unknown>): void;
|
|
120
|
+
captureError(error: unknown, extra?: Record<string, unknown>): void;
|
|
121
|
+
captureMessage(message: string, level?: 'info' | 'warn' | 'error'): void;
|
|
122
|
+
flush(): Promise<void>;
|
|
123
|
+
addCleanup(fn: () => void): void;
|
|
124
|
+
destroy(): void;
|
|
125
|
+
private resolveSessionId;
|
|
126
|
+
private resolveTraceId;
|
|
127
|
+
private pullRemoteConfig;
|
|
128
|
+
/** 上报用户资料;与日志队列隔离,避免资料重试或持久化到 IndexedDB。 */
|
|
129
|
+
private uploadUserProfile;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
interface AxiosLike {
|
|
133
|
+
interceptors: {
|
|
134
|
+
request: {
|
|
135
|
+
use: (fn: (c: AxiosConfigLike) => AxiosConfigLike) => void;
|
|
136
|
+
};
|
|
137
|
+
response: {
|
|
138
|
+
use: (onOk: (r: AxiosResponseLike) => AxiosResponseLike, onErr: (e: AxiosErrorLike) => Promise<never>) => void;
|
|
139
|
+
};
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
interface AxiosConfigLike {
|
|
143
|
+
url?: string;
|
|
144
|
+
method?: string;
|
|
145
|
+
metadata?: {
|
|
146
|
+
start: number;
|
|
147
|
+
requestBody?: string;
|
|
148
|
+
};
|
|
149
|
+
data?: unknown;
|
|
150
|
+
}
|
|
151
|
+
interface AxiosResponseLike {
|
|
152
|
+
status: number;
|
|
153
|
+
config: AxiosConfigLike;
|
|
154
|
+
}
|
|
155
|
+
interface AxiosErrorLike {
|
|
156
|
+
message: string;
|
|
157
|
+
config?: AxiosConfigLike;
|
|
158
|
+
response?: {
|
|
159
|
+
status: number;
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
declare function attachAxiosTo(logger: Logger, axios: AxiosLike): void;
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* 页面地址脱敏:仅掩码敏感查询参数的值,保留非敏感 query 与 hash 路由信息。
|
|
166
|
+
*
|
|
167
|
+
* 为什么不全量剥离 query/hash:业务页面的路由参数(如详情页 id、tab)是排查问题的
|
|
168
|
+
* 关键上下文,全量剥离会导致日志中的页面地址不完整、无法定位页面。
|
|
169
|
+
* 兼容 hash 内携带查询参数的路由形式(如 #/detail?token=xx)。
|
|
170
|
+
*
|
|
171
|
+
* @param url 待脱敏的页面地址(支持相对地址)
|
|
172
|
+
* @returns 脱敏后的地址;解析失败时原样返回,保证不影响上报
|
|
173
|
+
*/
|
|
174
|
+
declare function scrubUrl(url: string): string;
|
|
175
|
+
|
|
176
|
+
declare function init(options: LoggerOptions): Logger;
|
|
177
|
+
declare function setUser(userId: string, profile?: UserProfileInput): void;
|
|
178
|
+
/** 退出登录时调用,解除当前用户关联 */
|
|
179
|
+
declare function clearUser(): void;
|
|
180
|
+
declare function setTag(key: string, value: unknown): void;
|
|
181
|
+
declare function track(event: string, data?: Record<string, unknown>): void;
|
|
182
|
+
declare function captureError(error: unknown, extra?: Record<string, unknown>): void;
|
|
183
|
+
declare function captureMessage(message: string, level?: 'info' | 'warn' | 'error'): void;
|
|
184
|
+
declare function getSessionId(): string;
|
|
185
|
+
declare function getTraceId(): string;
|
|
186
|
+
declare function flush(): Promise<void>;
|
|
187
|
+
declare function destroy(): void;
|
|
188
|
+
declare function attachAxios(axios: Parameters<typeof attachAxiosTo>[1]): void;
|
|
189
|
+
|
|
190
|
+
declare const VuePlugin: {
|
|
191
|
+
install(app: any): void;
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
export { type LogEvent, LogType, Logger, type LoggerOptions, type SdkConfig, type UserProfileInput, VuePlugin, attachAxios, captureError, captureMessage, clearUser, destroy, flush, getSessionId, getTraceId, init, scrubUrl, setTag, setUser, track };
|