@mandujs/core 0.12.2 → 0.13.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.ko.md +304 -304
- package/README.md +653 -653
- package/package.json +1 -1
- package/src/brain/architecture/analyzer.ts +28 -26
- package/src/brain/doctor/analyzer.ts +1 -1
- package/src/bundler/build.ts +91 -91
- package/src/bundler/css.ts +302 -302
- package/src/bundler/dev.ts +0 -1
- package/src/change/history.ts +3 -3
- package/src/change/snapshot.ts +10 -9
- package/src/change/transaction.ts +2 -2
- package/src/client/Link.tsx +227 -227
- package/src/client/globals.ts +44 -44
- package/src/client/hooks.ts +267 -267
- package/src/client/index.ts +5 -5
- package/src/client/island.ts +8 -8
- package/src/client/router.ts +435 -435
- package/src/client/runtime.ts +23 -23
- package/src/client/serialize.ts +404 -404
- package/src/client/window-state.ts +101 -101
- package/src/config/mandu.ts +94 -96
- package/src/config/validate.ts +213 -215
- package/src/config/watcher.ts +311 -311
- package/src/constants.ts +40 -40
- package/src/content/content-layer.ts +314 -314
- package/src/content/content.test.ts +433 -433
- package/src/content/data-store.ts +245 -245
- package/src/content/digest.ts +133 -133
- package/src/content/index.ts +164 -164
- package/src/content/loader-context.ts +172 -172
- package/src/content/loaders/api.ts +216 -216
- package/src/content/loaders/file.ts +169 -169
- package/src/content/loaders/glob.ts +252 -252
- package/src/content/loaders/index.ts +34 -34
- package/src/content/loaders/types.ts +137 -137
- package/src/content/meta-store.ts +209 -209
- package/src/content/types.ts +282 -282
- package/src/content/watcher.ts +135 -135
- package/src/contract/client-safe.test.ts +42 -42
- package/src/contract/client-safe.ts +114 -114
- package/src/contract/client.ts +16 -16
- package/src/contract/define.ts +459 -459
- package/src/contract/handler.ts +10 -10
- package/src/contract/normalize.test.ts +276 -276
- package/src/contract/normalize.ts +404 -404
- package/src/contract/registry.test.ts +206 -206
- package/src/contract/registry.ts +568 -568
- package/src/contract/schema.ts +48 -48
- package/src/contract/types.ts +58 -58
- package/src/contract/validator.ts +32 -32
- package/src/devtools/ai/context-builder.ts +375 -375
- package/src/devtools/ai/index.ts +25 -25
- package/src/devtools/ai/mcp-connector.ts +465 -465
- package/src/devtools/client/catchers/error-catcher.ts +327 -327
- package/src/devtools/client/catchers/index.ts +18 -18
- package/src/devtools/client/catchers/network-proxy.ts +363 -363
- package/src/devtools/client/components/index.ts +39 -39
- package/src/devtools/client/components/kitchen-root.tsx +362 -362
- package/src/devtools/client/components/mandu-character.tsx +241 -241
- package/src/devtools/client/components/overlay.tsx +368 -368
- package/src/devtools/client/components/panel/errors-panel.tsx +259 -259
- package/src/devtools/client/components/panel/guard-panel.tsx +244 -244
- package/src/devtools/client/components/panel/index.ts +32 -32
- package/src/devtools/client/components/panel/islands-panel.tsx +304 -304
- package/src/devtools/client/components/panel/network-panel.tsx +292 -292
- package/src/devtools/client/components/panel/panel-container.tsx +259 -259
- package/src/devtools/client/filters/context-filters.ts +282 -282
- package/src/devtools/client/filters/index.ts +16 -16
- package/src/devtools/client/index.ts +63 -63
- package/src/devtools/client/persistence.ts +335 -335
- package/src/devtools/client/state-manager.ts +478 -478
- package/src/devtools/design-tokens.ts +263 -263
- package/src/devtools/hook/create-hook.ts +207 -207
- package/src/devtools/hook/index.ts +13 -13
- package/src/devtools/index.ts +439 -439
- package/src/devtools/init.ts +266 -266
- package/src/devtools/protocol.ts +237 -237
- package/src/devtools/server/index.ts +17 -17
- package/src/devtools/server/source-context.ts +444 -444
- package/src/devtools/types.ts +319 -319
- package/src/devtools/worker/index.ts +25 -25
- package/src/devtools/worker/redaction-worker.ts +222 -222
- package/src/devtools/worker/worker-manager.ts +409 -409
- package/src/error/classifier.ts +2 -2
- package/src/error/domains.ts +265 -265
- package/src/error/formatter.ts +32 -32
- package/src/error/result.ts +46 -46
- package/src/error/stack-analyzer.ts +5 -0
- package/src/error/types.ts +6 -6
- package/src/errors/extractor.ts +409 -409
- package/src/errors/index.ts +19 -19
- package/src/filling/auth.ts +308 -308
- package/src/filling/context.ts +569 -569
- package/src/filling/deps.ts +238 -238
- package/src/generator/contract-glue.ts +2 -1
- package/src/generator/generate.ts +12 -10
- package/src/generator/index.ts +3 -3
- package/src/generator/templates.ts +80 -79
- package/src/guard/analyzer.ts +360 -360
- package/src/guard/ast-analyzer.ts +806 -806
- package/src/guard/auto-correct.ts +1 -1
- package/src/guard/check.ts +128 -128
- package/src/guard/contract-guard.ts +9 -9
- package/src/guard/file-type.test.ts +24 -24
- package/src/guard/presets/atomic.ts +70 -70
- package/src/guard/presets/clean.ts +77 -77
- package/src/guard/presets/cqrs.test.ts +35 -14
- package/src/guard/presets/fsd.ts +79 -79
- package/src/guard/presets/hexagonal.ts +68 -68
- package/src/guard/presets/index.ts +291 -291
- package/src/guard/reporter.ts +445 -445
- package/src/guard/rules.ts +12 -12
- package/src/guard/statistics.ts +578 -578
- package/src/guard/suggestions.ts +358 -358
- package/src/guard/types.ts +348 -348
- package/src/guard/validator.ts +834 -834
- package/src/guard/watcher.ts +404 -404
- package/src/index.ts +1 -0
- package/src/intent/index.ts +310 -310
- package/src/island/index.ts +304 -304
- package/src/logging/index.ts +22 -22
- package/src/logging/transports.ts +365 -365
- package/src/paths.test.ts +47 -0
- package/src/paths.ts +47 -0
- package/src/plugins/index.ts +38 -38
- package/src/plugins/registry.ts +377 -377
- package/src/plugins/types.ts +363 -363
- package/src/report/build.ts +1 -1
- package/src/report/index.ts +1 -1
- package/src/router/fs-patterns.ts +387 -387
- package/src/router/fs-routes.ts +344 -401
- package/src/router/fs-scanner.ts +497 -497
- package/src/router/fs-types.ts +270 -278
- package/src/router/index.ts +81 -81
- package/src/runtime/boundary.tsx +232 -232
- package/src/runtime/compose.ts +222 -222
- package/src/runtime/lifecycle.ts +381 -381
- package/src/runtime/logger.test.ts +345 -345
- package/src/runtime/logger.ts +677 -677
- package/src/runtime/router.test.ts +476 -476
- package/src/runtime/router.ts +105 -105
- package/src/runtime/security.ts +155 -155
- package/src/runtime/server.ts +24 -24
- package/src/runtime/session-key.ts +328 -328
- package/src/runtime/ssr.ts +367 -367
- package/src/runtime/streaming-ssr.ts +1245 -1245
- package/src/runtime/trace.ts +144 -144
- package/src/seo/index.ts +214 -214
- package/src/seo/integration/ssr.ts +307 -307
- package/src/seo/render/basic.ts +427 -427
- package/src/seo/render/index.ts +143 -143
- package/src/seo/render/jsonld.ts +539 -539
- package/src/seo/render/opengraph.ts +191 -191
- package/src/seo/render/robots.ts +116 -116
- package/src/seo/render/sitemap.ts +137 -137
- package/src/seo/render/twitter.ts +126 -126
- package/src/seo/resolve/index.ts +353 -353
- package/src/seo/resolve/opengraph.ts +143 -143
- package/src/seo/resolve/robots.ts +73 -73
- package/src/seo/resolve/title.ts +94 -94
- package/src/seo/resolve/twitter.ts +73 -73
- package/src/seo/resolve/url.ts +97 -97
- package/src/seo/routes/index.ts +290 -290
- package/src/seo/types.ts +575 -575
- package/src/slot/validator.ts +39 -39
- package/src/spec/index.ts +3 -3
- package/src/spec/load.ts +76 -76
- package/src/spec/lock.ts +56 -56
- package/src/utils/bun.ts +8 -8
- package/src/utils/lru-cache.ts +75 -75
- package/src/utils/safe-io.ts +188 -188
- package/src/utils/string-safe.ts +298 -298
- package/src/watcher/rules.ts +5 -5
|
@@ -1,365 +1,365 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* DNA-008: Structured Logging - Transport System
|
|
3
|
-
*
|
|
4
|
-
* 로그 전송 레지스트리
|
|
5
|
-
* - 다중 전송 지원 (콘솔, 파일, 외부 서비스)
|
|
6
|
-
* - 동적 전송 추가/제거
|
|
7
|
-
* - 레벨별 필터링
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import type { LogLevel, LogEntry } from "../runtime/logger.js";
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* 로그 전송 레코드 (Transport에 전달되는 데이터)
|
|
14
|
-
*/
|
|
15
|
-
export interface LogTransportRecord {
|
|
16
|
-
/** 타임스탬프 (ISO 문자열) */
|
|
17
|
-
timestamp: string;
|
|
18
|
-
/** 로그 레벨 */
|
|
19
|
-
level: LogLevel;
|
|
20
|
-
/** 요청 ID */
|
|
21
|
-
requestId?: string;
|
|
22
|
-
/** HTTP 메서드 */
|
|
23
|
-
method?: string;
|
|
24
|
-
/** 요청 경로 */
|
|
25
|
-
path?: string;
|
|
26
|
-
/** HTTP 상태 코드 */
|
|
27
|
-
status?: number;
|
|
28
|
-
/** 응답 시간 (ms) */
|
|
29
|
-
duration?: number;
|
|
30
|
-
/** 에러 정보 */
|
|
31
|
-
error?: {
|
|
32
|
-
message: string;
|
|
33
|
-
stack?: string;
|
|
34
|
-
code?: string;
|
|
35
|
-
};
|
|
36
|
-
/** 커스텀 메타데이터 */
|
|
37
|
-
meta?: Record<string, unknown>;
|
|
38
|
-
/** 느린 요청 여부 */
|
|
39
|
-
slow?: boolean;
|
|
40
|
-
/** 원본 LogEntry (필요시 접근) */
|
|
41
|
-
raw?: LogEntry;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* 로그 전송 함수 타입
|
|
46
|
-
*/
|
|
47
|
-
export type LogTransport = (record: LogTransportRecord) => void | Promise<void>;
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* 전송 등록 정보
|
|
51
|
-
*/
|
|
52
|
-
export interface TransportRegistration {
|
|
53
|
-
/** 전송 ID */
|
|
54
|
-
id: string;
|
|
55
|
-
/** 전송 함수 */
|
|
56
|
-
transport: LogTransport;
|
|
57
|
-
/** 최소 로그 레벨 (이 레벨 이상만 전송) */
|
|
58
|
-
minLevel?: LogLevel;
|
|
59
|
-
/** 활성화 여부 */
|
|
60
|
-
enabled: boolean;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* 로그 레벨 우선순위
|
|
65
|
-
*/
|
|
66
|
-
const LEVEL_PRIORITY: Record<LogLevel, number> = {
|
|
67
|
-
debug: 0,
|
|
68
|
-
info: 1,
|
|
69
|
-
warn: 2,
|
|
70
|
-
error: 3,
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* 전역 전송 레지스트리
|
|
75
|
-
*/
|
|
76
|
-
class TransportRegistry {
|
|
77
|
-
private transports = new Map<string, TransportRegistration>();
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* 전송 추가
|
|
81
|
-
*
|
|
82
|
-
* @example
|
|
83
|
-
* ```ts
|
|
84
|
-
* attachLogTransport("file", async (record) => {
|
|
85
|
-
* await appendFile("app.log", JSON.stringify(record) + "\n");
|
|
86
|
-
* });
|
|
87
|
-
* ```
|
|
88
|
-
*/
|
|
89
|
-
attach(
|
|
90
|
-
id: string,
|
|
91
|
-
transport: LogTransport,
|
|
92
|
-
options: { minLevel?: LogLevel; enabled?: boolean } = {}
|
|
93
|
-
): void {
|
|
94
|
-
this.transports.set(id, {
|
|
95
|
-
id,
|
|
96
|
-
transport,
|
|
97
|
-
minLevel: options.minLevel,
|
|
98
|
-
enabled: options.enabled ?? true,
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* 전송 제거
|
|
104
|
-
*/
|
|
105
|
-
detach(id: string): boolean {
|
|
106
|
-
return this.transports.delete(id);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* 전송 활성화/비활성화
|
|
111
|
-
*/
|
|
112
|
-
setEnabled(id: string, enabled: boolean): void {
|
|
113
|
-
const registration = this.transports.get(id);
|
|
114
|
-
if (registration) {
|
|
115
|
-
registration.enabled = enabled;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* 모든 전송에 로그 전달
|
|
121
|
-
*/
|
|
122
|
-
async dispatch(record: LogTransportRecord): Promise<void> {
|
|
123
|
-
const recordLevel = LEVEL_PRIORITY[record.level];
|
|
124
|
-
|
|
125
|
-
const promises: Promise<void>[] = [];
|
|
126
|
-
|
|
127
|
-
for (const registration of this.transports.values()) {
|
|
128
|
-
if (!registration.enabled) continue;
|
|
129
|
-
|
|
130
|
-
// 레벨 필터링
|
|
131
|
-
if (registration.minLevel) {
|
|
132
|
-
const minLevel = LEVEL_PRIORITY[registration.minLevel];
|
|
133
|
-
if (recordLevel < minLevel) continue;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
try {
|
|
137
|
-
const result = registration.transport(record);
|
|
138
|
-
if (result instanceof Promise) {
|
|
139
|
-
promises.push(result.catch((err) => {
|
|
140
|
-
console.error(`[Log Transport] Error in ${registration.id}:`, err);
|
|
141
|
-
}));
|
|
142
|
-
}
|
|
143
|
-
} catch (err) {
|
|
144
|
-
console.error(`[Log Transport] Error in ${registration.id}:`, err);
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
await Promise.all(promises);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* 동기적으로 모든 전송에 로그 전달 (비동기 전송은 fire-and-forget)
|
|
153
|
-
*/
|
|
154
|
-
dispatchSync(record: LogTransportRecord): void {
|
|
155
|
-
const recordLevel = LEVEL_PRIORITY[record.level];
|
|
156
|
-
|
|
157
|
-
for (const registration of this.transports.values()) {
|
|
158
|
-
if (!registration.enabled) continue;
|
|
159
|
-
|
|
160
|
-
if (registration.minLevel) {
|
|
161
|
-
const minLevel = LEVEL_PRIORITY[registration.minLevel];
|
|
162
|
-
if (recordLevel < minLevel) continue;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
try {
|
|
166
|
-
const result = registration.transport(record);
|
|
167
|
-
if (result instanceof Promise) {
|
|
168
|
-
// 비동기 결과는 무시 (fire-and-forget)
|
|
169
|
-
result.catch((err) => {
|
|
170
|
-
console.error(`[Log Transport] Error in ${registration.id}:`, err);
|
|
171
|
-
});
|
|
172
|
-
}
|
|
173
|
-
} catch (err) {
|
|
174
|
-
console.error(`[Log Transport] Error in ${registration.id}:`, err);
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
* 등록된 전송 목록
|
|
181
|
-
*/
|
|
182
|
-
list(): TransportRegistration[] {
|
|
183
|
-
return Array.from(this.transports.values());
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
/**
|
|
187
|
-
* 전송 존재 여부
|
|
188
|
-
*/
|
|
189
|
-
has(id: string): boolean {
|
|
190
|
-
return this.transports.has(id);
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
/**
|
|
194
|
-
* 모든 전송 제거
|
|
195
|
-
*/
|
|
196
|
-
clear(): void {
|
|
197
|
-
this.transports.clear();
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* 전송 개수
|
|
202
|
-
*/
|
|
203
|
-
get size(): number {
|
|
204
|
-
return this.transports.size;
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
/**
|
|
209
|
-
* 전역 전송 레지스트리 인스턴스
|
|
210
|
-
*/
|
|
211
|
-
export const transportRegistry = new TransportRegistry();
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* 로그 전송 추가 (편의 함수)
|
|
215
|
-
*
|
|
216
|
-
* @example
|
|
217
|
-
* ```ts
|
|
218
|
-
* // 파일 전송
|
|
219
|
-
* attachLogTransport("file", async (record) => {
|
|
220
|
-
* await fs.appendFile("app.log", JSON.stringify(record) + "\n");
|
|
221
|
-
* }, { minLevel: "info" });
|
|
222
|
-
*
|
|
223
|
-
* // 외부 서비스 전송
|
|
224
|
-
* attachLogTransport("datadog", async (record) => {
|
|
225
|
-
* await fetch("https://http-intake.logs.datadoghq.com/...", {
|
|
226
|
-
* method: "POST",
|
|
227
|
-
* body: JSON.stringify(record),
|
|
228
|
-
* });
|
|
229
|
-
* }, { minLevel: "warn" });
|
|
230
|
-
* ```
|
|
231
|
-
*/
|
|
232
|
-
export function attachLogTransport(
|
|
233
|
-
id: string,
|
|
234
|
-
transport: LogTransport,
|
|
235
|
-
options?: { minLevel?: LogLevel; enabled?: boolean }
|
|
236
|
-
): void {
|
|
237
|
-
transportRegistry.attach(id, transport, options);
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
/**
|
|
241
|
-
* 로그 전송 제거 (편의 함수)
|
|
242
|
-
*/
|
|
243
|
-
export function detachLogTransport(id: string): boolean {
|
|
244
|
-
return transportRegistry.detach(id);
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
/**
|
|
248
|
-
* LogEntry를 LogTransportRecord로 변환
|
|
249
|
-
*/
|
|
250
|
-
export function entryToTransportRecord(entry: LogEntry): LogTransportRecord {
|
|
251
|
-
return {
|
|
252
|
-
timestamp: entry.timestamp,
|
|
253
|
-
level: entry.level,
|
|
254
|
-
requestId: entry.requestId,
|
|
255
|
-
method: entry.method,
|
|
256
|
-
path: entry.path,
|
|
257
|
-
status: entry.status,
|
|
258
|
-
duration: entry.duration,
|
|
259
|
-
error: entry.error ? {
|
|
260
|
-
message: entry.error.message,
|
|
261
|
-
stack: entry.error.stack,
|
|
262
|
-
} : undefined,
|
|
263
|
-
slow: entry.slow,
|
|
264
|
-
raw: entry,
|
|
265
|
-
};
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
// ============================================
|
|
269
|
-
// Built-in Transports
|
|
270
|
-
// ============================================
|
|
271
|
-
|
|
272
|
-
/**
|
|
273
|
-
* 콘솔 전송 (기본)
|
|
274
|
-
*/
|
|
275
|
-
export function createConsoleTransport(options: {
|
|
276
|
-
format?: "json" | "pretty";
|
|
277
|
-
} = {}): LogTransport {
|
|
278
|
-
const { format = "pretty" } = options;
|
|
279
|
-
|
|
280
|
-
return (record) => {
|
|
281
|
-
const output = format === "json"
|
|
282
|
-
? JSON.stringify(record)
|
|
283
|
-
: `[${record.timestamp}] ${record.level.toUpperCase()} ${record.method ?? ""} ${record.path ?? ""} ${record.status ?? ""} ${record.duration ? record.duration.toFixed(0) + "ms" : ""}`;
|
|
284
|
-
|
|
285
|
-
switch (record.level) {
|
|
286
|
-
case "error":
|
|
287
|
-
console.error(output);
|
|
288
|
-
break;
|
|
289
|
-
case "warn":
|
|
290
|
-
console.warn(output);
|
|
291
|
-
break;
|
|
292
|
-
default:
|
|
293
|
-
console.log(output);
|
|
294
|
-
}
|
|
295
|
-
};
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
/**
|
|
299
|
-
* 메모리 버퍼 전송 (테스트용)
|
|
300
|
-
*/
|
|
301
|
-
export function createBufferTransport(buffer: LogTransportRecord[]): LogTransport {
|
|
302
|
-
return (record) => {
|
|
303
|
-
buffer.push(record);
|
|
304
|
-
};
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
/**
|
|
308
|
-
* 필터링 전송 래퍼
|
|
309
|
-
*/
|
|
310
|
-
export function createFilteredTransport(
|
|
311
|
-
transport: LogTransport,
|
|
312
|
-
filter: (record: LogTransportRecord) => boolean
|
|
313
|
-
): LogTransport {
|
|
314
|
-
return (record) => {
|
|
315
|
-
if (filter(record)) {
|
|
316
|
-
return transport(record);
|
|
317
|
-
}
|
|
318
|
-
};
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
/**
|
|
322
|
-
* 배치 전송 (성능 최적화)
|
|
323
|
-
*/
|
|
324
|
-
export function createBatchTransport(
|
|
325
|
-
flush: (records: LogTransportRecord[]) => void | Promise<void>,
|
|
326
|
-
options: {
|
|
327
|
-
maxSize?: number;
|
|
328
|
-
flushInterval?: number;
|
|
329
|
-
} = {}
|
|
330
|
-
): { transport: LogTransport; flush: () => Promise<void>; stop: () => void } {
|
|
331
|
-
const { maxSize = 100, flushInterval = 5000 } = options;
|
|
332
|
-
|
|
333
|
-
const buffer: LogTransportRecord[] = [];
|
|
334
|
-
let timer: ReturnType<typeof setInterval> | null = null;
|
|
335
|
-
|
|
336
|
-
const doFlush = async () => {
|
|
337
|
-
if (buffer.length === 0) return;
|
|
338
|
-
const records = buffer.splice(0, buffer.length);
|
|
339
|
-
await flush(records);
|
|
340
|
-
};
|
|
341
|
-
|
|
342
|
-
timer = setInterval(() => {
|
|
343
|
-
doFlush().catch((err) => {
|
|
344
|
-
console.error("[Batch Transport] Flush error:", err);
|
|
345
|
-
});
|
|
346
|
-
}, flushInterval);
|
|
347
|
-
|
|
348
|
-
return {
|
|
349
|
-
transport: (record) => {
|
|
350
|
-
buffer.push(record);
|
|
351
|
-
if (buffer.length >= maxSize) {
|
|
352
|
-
doFlush().catch((err) => {
|
|
353
|
-
console.error("[Batch Transport] Flush error:", err);
|
|
354
|
-
});
|
|
355
|
-
}
|
|
356
|
-
},
|
|
357
|
-
flush: doFlush,
|
|
358
|
-
stop: () => {
|
|
359
|
-
if (timer) {
|
|
360
|
-
clearInterval(timer);
|
|
361
|
-
timer = null;
|
|
362
|
-
}
|
|
363
|
-
},
|
|
364
|
-
};
|
|
365
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* DNA-008: Structured Logging - Transport System
|
|
3
|
+
*
|
|
4
|
+
* 로그 전송 레지스트리
|
|
5
|
+
* - 다중 전송 지원 (콘솔, 파일, 외부 서비스)
|
|
6
|
+
* - 동적 전송 추가/제거
|
|
7
|
+
* - 레벨별 필터링
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { LogLevel, LogEntry } from "../runtime/logger.js";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 로그 전송 레코드 (Transport에 전달되는 데이터)
|
|
14
|
+
*/
|
|
15
|
+
export interface LogTransportRecord {
|
|
16
|
+
/** 타임스탬프 (ISO 문자열) */
|
|
17
|
+
timestamp: string;
|
|
18
|
+
/** 로그 레벨 */
|
|
19
|
+
level: LogLevel;
|
|
20
|
+
/** 요청 ID */
|
|
21
|
+
requestId?: string;
|
|
22
|
+
/** HTTP 메서드 */
|
|
23
|
+
method?: string;
|
|
24
|
+
/** 요청 경로 */
|
|
25
|
+
path?: string;
|
|
26
|
+
/** HTTP 상태 코드 */
|
|
27
|
+
status?: number;
|
|
28
|
+
/** 응답 시간 (ms) */
|
|
29
|
+
duration?: number;
|
|
30
|
+
/** 에러 정보 */
|
|
31
|
+
error?: {
|
|
32
|
+
message: string;
|
|
33
|
+
stack?: string;
|
|
34
|
+
code?: string;
|
|
35
|
+
};
|
|
36
|
+
/** 커스텀 메타데이터 */
|
|
37
|
+
meta?: Record<string, unknown>;
|
|
38
|
+
/** 느린 요청 여부 */
|
|
39
|
+
slow?: boolean;
|
|
40
|
+
/** 원본 LogEntry (필요시 접근) */
|
|
41
|
+
raw?: LogEntry;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* 로그 전송 함수 타입
|
|
46
|
+
*/
|
|
47
|
+
export type LogTransport = (record: LogTransportRecord) => void | Promise<void>;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 전송 등록 정보
|
|
51
|
+
*/
|
|
52
|
+
export interface TransportRegistration {
|
|
53
|
+
/** 전송 ID */
|
|
54
|
+
id: string;
|
|
55
|
+
/** 전송 함수 */
|
|
56
|
+
transport: LogTransport;
|
|
57
|
+
/** 최소 로그 레벨 (이 레벨 이상만 전송) */
|
|
58
|
+
minLevel?: LogLevel;
|
|
59
|
+
/** 활성화 여부 */
|
|
60
|
+
enabled: boolean;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 로그 레벨 우선순위
|
|
65
|
+
*/
|
|
66
|
+
const LEVEL_PRIORITY: Record<LogLevel, number> = {
|
|
67
|
+
debug: 0,
|
|
68
|
+
info: 1,
|
|
69
|
+
warn: 2,
|
|
70
|
+
error: 3,
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* 전역 전송 레지스트리
|
|
75
|
+
*/
|
|
76
|
+
class TransportRegistry {
|
|
77
|
+
private transports = new Map<string, TransportRegistration>();
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* 전송 추가
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```ts
|
|
84
|
+
* attachLogTransport("file", async (record) => {
|
|
85
|
+
* await appendFile("app.log", JSON.stringify(record) + "\n");
|
|
86
|
+
* });
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
attach(
|
|
90
|
+
id: string,
|
|
91
|
+
transport: LogTransport,
|
|
92
|
+
options: { minLevel?: LogLevel; enabled?: boolean } = {}
|
|
93
|
+
): void {
|
|
94
|
+
this.transports.set(id, {
|
|
95
|
+
id,
|
|
96
|
+
transport,
|
|
97
|
+
minLevel: options.minLevel,
|
|
98
|
+
enabled: options.enabled ?? true,
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* 전송 제거
|
|
104
|
+
*/
|
|
105
|
+
detach(id: string): boolean {
|
|
106
|
+
return this.transports.delete(id);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* 전송 활성화/비활성화
|
|
111
|
+
*/
|
|
112
|
+
setEnabled(id: string, enabled: boolean): void {
|
|
113
|
+
const registration = this.transports.get(id);
|
|
114
|
+
if (registration) {
|
|
115
|
+
registration.enabled = enabled;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* 모든 전송에 로그 전달
|
|
121
|
+
*/
|
|
122
|
+
async dispatch(record: LogTransportRecord): Promise<void> {
|
|
123
|
+
const recordLevel = LEVEL_PRIORITY[record.level];
|
|
124
|
+
|
|
125
|
+
const promises: Promise<void>[] = [];
|
|
126
|
+
|
|
127
|
+
for (const registration of this.transports.values()) {
|
|
128
|
+
if (!registration.enabled) continue;
|
|
129
|
+
|
|
130
|
+
// 레벨 필터링
|
|
131
|
+
if (registration.minLevel) {
|
|
132
|
+
const minLevel = LEVEL_PRIORITY[registration.minLevel];
|
|
133
|
+
if (recordLevel < minLevel) continue;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
try {
|
|
137
|
+
const result = registration.transport(record);
|
|
138
|
+
if (result instanceof Promise) {
|
|
139
|
+
promises.push(result.catch((err) => {
|
|
140
|
+
console.error(`[Log Transport] Error in ${registration.id}:`, err);
|
|
141
|
+
}));
|
|
142
|
+
}
|
|
143
|
+
} catch (err) {
|
|
144
|
+
console.error(`[Log Transport] Error in ${registration.id}:`, err);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
await Promise.all(promises);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* 동기적으로 모든 전송에 로그 전달 (비동기 전송은 fire-and-forget)
|
|
153
|
+
*/
|
|
154
|
+
dispatchSync(record: LogTransportRecord): void {
|
|
155
|
+
const recordLevel = LEVEL_PRIORITY[record.level];
|
|
156
|
+
|
|
157
|
+
for (const registration of this.transports.values()) {
|
|
158
|
+
if (!registration.enabled) continue;
|
|
159
|
+
|
|
160
|
+
if (registration.minLevel) {
|
|
161
|
+
const minLevel = LEVEL_PRIORITY[registration.minLevel];
|
|
162
|
+
if (recordLevel < minLevel) continue;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
try {
|
|
166
|
+
const result = registration.transport(record);
|
|
167
|
+
if (result instanceof Promise) {
|
|
168
|
+
// 비동기 결과는 무시 (fire-and-forget)
|
|
169
|
+
result.catch((err) => {
|
|
170
|
+
console.error(`[Log Transport] Error in ${registration.id}:`, err);
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
} catch (err) {
|
|
174
|
+
console.error(`[Log Transport] Error in ${registration.id}:`, err);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* 등록된 전송 목록
|
|
181
|
+
*/
|
|
182
|
+
list(): TransportRegistration[] {
|
|
183
|
+
return Array.from(this.transports.values());
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* 전송 존재 여부
|
|
188
|
+
*/
|
|
189
|
+
has(id: string): boolean {
|
|
190
|
+
return this.transports.has(id);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* 모든 전송 제거
|
|
195
|
+
*/
|
|
196
|
+
clear(): void {
|
|
197
|
+
this.transports.clear();
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* 전송 개수
|
|
202
|
+
*/
|
|
203
|
+
get size(): number {
|
|
204
|
+
return this.transports.size;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* 전역 전송 레지스트리 인스턴스
|
|
210
|
+
*/
|
|
211
|
+
export const transportRegistry = new TransportRegistry();
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* 로그 전송 추가 (편의 함수)
|
|
215
|
+
*
|
|
216
|
+
* @example
|
|
217
|
+
* ```ts
|
|
218
|
+
* // 파일 전송
|
|
219
|
+
* attachLogTransport("file", async (record) => {
|
|
220
|
+
* await fs.appendFile("app.log", JSON.stringify(record) + "\n");
|
|
221
|
+
* }, { minLevel: "info" });
|
|
222
|
+
*
|
|
223
|
+
* // 외부 서비스 전송
|
|
224
|
+
* attachLogTransport("datadog", async (record) => {
|
|
225
|
+
* await fetch("https://http-intake.logs.datadoghq.com/...", {
|
|
226
|
+
* method: "POST",
|
|
227
|
+
* body: JSON.stringify(record),
|
|
228
|
+
* });
|
|
229
|
+
* }, { minLevel: "warn" });
|
|
230
|
+
* ```
|
|
231
|
+
*/
|
|
232
|
+
export function attachLogTransport(
|
|
233
|
+
id: string,
|
|
234
|
+
transport: LogTransport,
|
|
235
|
+
options?: { minLevel?: LogLevel; enabled?: boolean }
|
|
236
|
+
): void {
|
|
237
|
+
transportRegistry.attach(id, transport, options);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* 로그 전송 제거 (편의 함수)
|
|
242
|
+
*/
|
|
243
|
+
export function detachLogTransport(id: string): boolean {
|
|
244
|
+
return transportRegistry.detach(id);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* LogEntry를 LogTransportRecord로 변환
|
|
249
|
+
*/
|
|
250
|
+
export function entryToTransportRecord(entry: LogEntry): LogTransportRecord {
|
|
251
|
+
return {
|
|
252
|
+
timestamp: entry.timestamp,
|
|
253
|
+
level: entry.level,
|
|
254
|
+
requestId: entry.requestId,
|
|
255
|
+
method: entry.method,
|
|
256
|
+
path: entry.path,
|
|
257
|
+
status: entry.status,
|
|
258
|
+
duration: entry.duration,
|
|
259
|
+
error: entry.error ? {
|
|
260
|
+
message: entry.error.message,
|
|
261
|
+
stack: entry.error.stack,
|
|
262
|
+
} : undefined,
|
|
263
|
+
slow: entry.slow,
|
|
264
|
+
raw: entry,
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// ============================================
|
|
269
|
+
// Built-in Transports
|
|
270
|
+
// ============================================
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* 콘솔 전송 (기본)
|
|
274
|
+
*/
|
|
275
|
+
export function createConsoleTransport(options: {
|
|
276
|
+
format?: "json" | "pretty";
|
|
277
|
+
} = {}): LogTransport {
|
|
278
|
+
const { format = "pretty" } = options;
|
|
279
|
+
|
|
280
|
+
return (record) => {
|
|
281
|
+
const output = format === "json"
|
|
282
|
+
? JSON.stringify(record)
|
|
283
|
+
: `[${record.timestamp}] ${record.level.toUpperCase()} ${record.method ?? ""} ${record.path ?? ""} ${record.status ?? ""} ${record.duration ? record.duration.toFixed(0) + "ms" : ""}`;
|
|
284
|
+
|
|
285
|
+
switch (record.level) {
|
|
286
|
+
case "error":
|
|
287
|
+
console.error(output);
|
|
288
|
+
break;
|
|
289
|
+
case "warn":
|
|
290
|
+
console.warn(output);
|
|
291
|
+
break;
|
|
292
|
+
default:
|
|
293
|
+
console.log(output);
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* 메모리 버퍼 전송 (테스트용)
|
|
300
|
+
*/
|
|
301
|
+
export function createBufferTransport(buffer: LogTransportRecord[]): LogTransport {
|
|
302
|
+
return (record) => {
|
|
303
|
+
buffer.push(record);
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* 필터링 전송 래퍼
|
|
309
|
+
*/
|
|
310
|
+
export function createFilteredTransport(
|
|
311
|
+
transport: LogTransport,
|
|
312
|
+
filter: (record: LogTransportRecord) => boolean
|
|
313
|
+
): LogTransport {
|
|
314
|
+
return (record) => {
|
|
315
|
+
if (filter(record)) {
|
|
316
|
+
return transport(record);
|
|
317
|
+
}
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* 배치 전송 (성능 최적화)
|
|
323
|
+
*/
|
|
324
|
+
export function createBatchTransport(
|
|
325
|
+
flush: (records: LogTransportRecord[]) => void | Promise<void>,
|
|
326
|
+
options: {
|
|
327
|
+
maxSize?: number;
|
|
328
|
+
flushInterval?: number;
|
|
329
|
+
} = {}
|
|
330
|
+
): { transport: LogTransport; flush: () => Promise<void>; stop: () => void } {
|
|
331
|
+
const { maxSize = 100, flushInterval = 5000 } = options;
|
|
332
|
+
|
|
333
|
+
const buffer: LogTransportRecord[] = [];
|
|
334
|
+
let timer: ReturnType<typeof setInterval> | null = null;
|
|
335
|
+
|
|
336
|
+
const doFlush = async () => {
|
|
337
|
+
if (buffer.length === 0) return;
|
|
338
|
+
const records = buffer.splice(0, buffer.length);
|
|
339
|
+
await flush(records);
|
|
340
|
+
};
|
|
341
|
+
|
|
342
|
+
timer = setInterval(() => {
|
|
343
|
+
doFlush().catch((err) => {
|
|
344
|
+
console.error("[Batch Transport] Flush error:", err);
|
|
345
|
+
});
|
|
346
|
+
}, flushInterval);
|
|
347
|
+
|
|
348
|
+
return {
|
|
349
|
+
transport: (record) => {
|
|
350
|
+
buffer.push(record);
|
|
351
|
+
if (buffer.length >= maxSize) {
|
|
352
|
+
doFlush().catch((err) => {
|
|
353
|
+
console.error("[Batch Transport] Flush error:", err);
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
},
|
|
357
|
+
flush: doFlush,
|
|
358
|
+
stop: () => {
|
|
359
|
+
if (timer) {
|
|
360
|
+
clearInterval(timer);
|
|
361
|
+
timer = null;
|
|
362
|
+
}
|
|
363
|
+
},
|
|
364
|
+
};
|
|
365
|
+
}
|