@southwind-ai/shared 0.3.0 → 0.3.1
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/index.ts +1 -0
- package/package.json +3 -2
- package/src/structured-log.ts +215 -0
package/index.ts
CHANGED
|
@@ -17,6 +17,7 @@ export * from "./src/migration-bundle-validation.js";
|
|
|
17
17
|
export * from "./src/mcp.js";
|
|
18
18
|
export * from "./src/module-configuration.js";
|
|
19
19
|
export * from "./src/correlation.js";
|
|
20
|
+
export * from "./src/structured-log.js";
|
|
20
21
|
export * from "./src/notification.js";
|
|
21
22
|
export * from "./src/password.js";
|
|
22
23
|
export * from "./src/platform-settings.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@southwind-ai/shared",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"license": "UNLICENSED",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -15,7 +15,8 @@
|
|
|
15
15
|
"typecheck": "tsc --noEmit --project tsconfig.json"
|
|
16
16
|
},
|
|
17
17
|
"exports": {
|
|
18
|
-
".": "./index.ts"
|
|
18
|
+
".": "./index.ts",
|
|
19
|
+
"./structured-log": "./src/structured-log.ts"
|
|
19
20
|
},
|
|
20
21
|
"publishConfig": {
|
|
21
22
|
"access": "public"
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
/** 运行日志只承载排障线索;业务审计和 Agent Run 事实使用各自的持久化入口。 */
|
|
2
|
+
export const STRUCTURED_LOG_MAX_BYTES = 2048;
|
|
3
|
+
|
|
4
|
+
export type StructuredLogLevel = "info" | "warn" | "error";
|
|
5
|
+
export type StructuredLogOutcome = "success" | "failure" | "denied";
|
|
6
|
+
|
|
7
|
+
export interface StructuredLogInput {
|
|
8
|
+
event: string;
|
|
9
|
+
requestId?: string;
|
|
10
|
+
runId?: string;
|
|
11
|
+
jobId?: string;
|
|
12
|
+
traceId?: string;
|
|
13
|
+
diagnosticId?: string;
|
|
14
|
+
outcome?: StructuredLogOutcome;
|
|
15
|
+
errorCode?: string;
|
|
16
|
+
stageKey?: string;
|
|
17
|
+
handlerKey?: string;
|
|
18
|
+
queue?: string;
|
|
19
|
+
attemptNumber?: number;
|
|
20
|
+
method?: string;
|
|
21
|
+
/** 只接受框架登记的路由模板,不能传请求 URL 或实际对象路径。 */
|
|
22
|
+
route?: string;
|
|
23
|
+
statusCode?: number;
|
|
24
|
+
durationMs?: number;
|
|
25
|
+
actorType?: string;
|
|
26
|
+
authority?: string;
|
|
27
|
+
port?: number;
|
|
28
|
+
created?: number;
|
|
29
|
+
metadataUpdated?: number;
|
|
30
|
+
archived?: number;
|
|
31
|
+
unchanged?: number;
|
|
32
|
+
queued?: number;
|
|
33
|
+
dropped?: number;
|
|
34
|
+
completedUnits?: number;
|
|
35
|
+
totalUnits?: number;
|
|
36
|
+
count?: number;
|
|
37
|
+
/** 兼容现有 Logger 接口;未知字段在序列化时一律丢弃。 */
|
|
38
|
+
[key: string]: unknown;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface StructuredLogSink {
|
|
42
|
+
info(record: Record<string, unknown>): void;
|
|
43
|
+
warn(record: Record<string, unknown>): void;
|
|
44
|
+
error(record: Record<string, unknown>): void;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface StructuredLogger {
|
|
48
|
+
info(input: StructuredLogInput, message?: string): void;
|
|
49
|
+
warn(input: StructuredLogInput, message?: string): void;
|
|
50
|
+
error(input: StructuredLogInput, message?: string): void;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const stableKey = /^[a-z][a-z0-9]*(?:[._-][a-z0-9]+)*$/;
|
|
54
|
+
const correlationKey = /^[A-Za-z0-9._-]{1,128}$/;
|
|
55
|
+
const errorKey = /^[A-Z][A-Z0-9_]{0,95}$/;
|
|
56
|
+
const methodKey = /^[A-Z]{1,12}$/;
|
|
57
|
+
const routeTemplate =
|
|
58
|
+
/^\/(?:[A-Za-z][A-Za-z0-9_-]*|:[A-Za-z][A-Za-z0-9_]*|\*)(?:\/(?:[A-Za-z][A-Za-z0-9_-]*|:[A-Za-z][A-Za-z0-9_]*|\*))*$/;
|
|
59
|
+
|
|
60
|
+
/** Adapter 可接 Pino、stdout 或测试 Sink;自由文本 message 不进入日志。 */
|
|
61
|
+
export function createStructuredLogger(
|
|
62
|
+
sink: StructuredLogSink,
|
|
63
|
+
component: string,
|
|
64
|
+
now: () => Date = () => new Date(),
|
|
65
|
+
): StructuredLogger {
|
|
66
|
+
if (!stableKey.test(component) || component.length > 64) {
|
|
67
|
+
throw new Error("运行日志 component 必须是有界稳定 Key");
|
|
68
|
+
}
|
|
69
|
+
const emit = (level: StructuredLogLevel, input: StructuredLogInput) => {
|
|
70
|
+
const record = structuredLogRecord(level, component, input, now());
|
|
71
|
+
try {
|
|
72
|
+
sink[level](record);
|
|
73
|
+
} catch {
|
|
74
|
+
// 排障日志是旁路输出;审计和业务结果由各自的可靠存储决定。
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
return {
|
|
78
|
+
info: (input) => emit("info", input),
|
|
79
|
+
warn: (input) => emit("warn", input),
|
|
80
|
+
error: (input) => emit("error", input),
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function structuredLogRecord(
|
|
85
|
+
level: StructuredLogLevel,
|
|
86
|
+
component: string,
|
|
87
|
+
input: StructuredLogInput,
|
|
88
|
+
at: Date,
|
|
89
|
+
): Record<string, unknown> {
|
|
90
|
+
const record: Record<string, unknown> = {
|
|
91
|
+
timestamp: at.toISOString(),
|
|
92
|
+
level,
|
|
93
|
+
component,
|
|
94
|
+
event:
|
|
95
|
+
stableKey.test(input.event) && input.event.length <= 96
|
|
96
|
+
? input.event
|
|
97
|
+
: "logging.event.invalid",
|
|
98
|
+
retentionClass: "operational",
|
|
99
|
+
};
|
|
100
|
+
for (const key of [
|
|
101
|
+
"requestId",
|
|
102
|
+
"runId",
|
|
103
|
+
"jobId",
|
|
104
|
+
"traceId",
|
|
105
|
+
"diagnosticId",
|
|
106
|
+
] as const) {
|
|
107
|
+
const value = input[key];
|
|
108
|
+
if (typeof value === "string" && correlationKey.test(value)) {
|
|
109
|
+
record[key] = value;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
if (
|
|
113
|
+
input.outcome === "success" ||
|
|
114
|
+
input.outcome === "failure" ||
|
|
115
|
+
input.outcome === "denied"
|
|
116
|
+
)
|
|
117
|
+
record.outcome = input.outcome;
|
|
118
|
+
if (typeof input.errorCode === "string" && errorKey.test(input.errorCode)) {
|
|
119
|
+
record.errorCode = input.errorCode;
|
|
120
|
+
}
|
|
121
|
+
for (const key of [
|
|
122
|
+
"stageKey",
|
|
123
|
+
"handlerKey",
|
|
124
|
+
"queue",
|
|
125
|
+
"actorType",
|
|
126
|
+
"authority",
|
|
127
|
+
] as const) {
|
|
128
|
+
const value = input[key];
|
|
129
|
+
if (
|
|
130
|
+
typeof value === "string" &&
|
|
131
|
+
value.length <= 64 &&
|
|
132
|
+
stableKey.test(value)
|
|
133
|
+
) {
|
|
134
|
+
record[key] = value;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
if (typeof input.method === "string" && methodKey.test(input.method)) {
|
|
138
|
+
record.method = input.method;
|
|
139
|
+
}
|
|
140
|
+
if (
|
|
141
|
+
typeof input.route === "string" &&
|
|
142
|
+
input.route.length <= 160 &&
|
|
143
|
+
(input.route === "/" || routeTemplate.test(input.route))
|
|
144
|
+
)
|
|
145
|
+
record.route = input.route;
|
|
146
|
+
if (validNumber(input.statusCode, 100, 599, true))
|
|
147
|
+
record.statusCode = input.statusCode;
|
|
148
|
+
if (validNumber(input.durationMs, 0, 3_600_000))
|
|
149
|
+
record.durationMs = input.durationMs;
|
|
150
|
+
if (validNumber(input.port, 1, 65535, true)) record.port = input.port;
|
|
151
|
+
if (validNumber(input.attemptNumber, 1, 1_000_000_000, true))
|
|
152
|
+
record.attemptNumber = input.attemptNumber;
|
|
153
|
+
for (const key of [
|
|
154
|
+
"created",
|
|
155
|
+
"metadataUpdated",
|
|
156
|
+
"archived",
|
|
157
|
+
"unchanged",
|
|
158
|
+
"queued",
|
|
159
|
+
"dropped",
|
|
160
|
+
"completedUnits",
|
|
161
|
+
"totalUnits",
|
|
162
|
+
"count",
|
|
163
|
+
] as const) {
|
|
164
|
+
const value = input[key];
|
|
165
|
+
if (validNumber(value, 0, 1_000_000_000, true)) record[key] = value;
|
|
166
|
+
}
|
|
167
|
+
for (const key of [
|
|
168
|
+
"dropped",
|
|
169
|
+
"completedUnits",
|
|
170
|
+
"totalUnits",
|
|
171
|
+
"count",
|
|
172
|
+
"route",
|
|
173
|
+
"queued",
|
|
174
|
+
"unchanged",
|
|
175
|
+
"archived",
|
|
176
|
+
"metadataUpdated",
|
|
177
|
+
"created",
|
|
178
|
+
"port",
|
|
179
|
+
"attemptNumber",
|
|
180
|
+
"queue",
|
|
181
|
+
"handlerKey",
|
|
182
|
+
"authority",
|
|
183
|
+
"actorType",
|
|
184
|
+
"stageKey",
|
|
185
|
+
"diagnosticId",
|
|
186
|
+
"traceId",
|
|
187
|
+
"jobId",
|
|
188
|
+
"runId",
|
|
189
|
+
"requestId",
|
|
190
|
+
] as const) {
|
|
191
|
+
if (
|
|
192
|
+
new TextEncoder().encode(JSON.stringify(record)).length <=
|
|
193
|
+
STRUCTURED_LOG_MAX_BYTES
|
|
194
|
+
)
|
|
195
|
+
break;
|
|
196
|
+
delete record[key];
|
|
197
|
+
record.truncated = true;
|
|
198
|
+
}
|
|
199
|
+
return record;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function validNumber(
|
|
203
|
+
value: unknown,
|
|
204
|
+
min: number,
|
|
205
|
+
max: number,
|
|
206
|
+
integer = false,
|
|
207
|
+
): value is number {
|
|
208
|
+
return (
|
|
209
|
+
typeof value === "number" &&
|
|
210
|
+
Number.isFinite(value) &&
|
|
211
|
+
value >= min &&
|
|
212
|
+
value <= max &&
|
|
213
|
+
(!integer || Number.isInteger(value))
|
|
214
|
+
);
|
|
215
|
+
}
|