@renxqoo/renx-code 0.0.4 → 0.0.6
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 +82 -51
- package/bin/renx.cjs +16 -0
- package/package.json +2 -45
- package/src/agent/runtime/runtime.context-usage.test.ts +4 -5
- package/src/agent/runtime/runtime.error-handling.test.ts +4 -5
- package/src/agent/runtime/runtime.test.ts +7 -4
- package/src/agent/runtime/runtime.ts +3 -9
- package/src/agent/runtime/runtime.usage-forwarding.test.ts +4 -5
- package/src/agent/runtime/source-modules.test.ts +16 -35
- package/src/agent/runtime/source-modules.ts +17 -0
- package/vendor/agent-root/src/agent/ENTERPRISE_ACCEPTANCE_CHECKLIST.md +95 -0
- package/vendor/agent-root/src/agent/ENTERPRISE_REALTIME.html +1345 -0
- package/vendor/agent-root/src/agent/ENTERPRISE_REALTIME.md +1353 -0
- package/vendor/agent-root/src/agent/ERROR_CONTRACT.md +60 -0
- package/vendor/agent-root/src/agent/TEST_COVERAGE_ANALYSIS.md +278 -0
- package/vendor/agent-root/src/agent/__test__/error-contract.test.ts +72 -0
- package/vendor/agent-root/src/agent/__test__/types.test.ts +137 -0
- package/vendor/agent-root/src/agent/agent/__test__/abort-runtime.test.ts +83 -0
- package/vendor/agent-root/src/agent/agent/__test__/callback-safety.test.ts +34 -0
- package/vendor/agent-root/src/agent/agent/__test__/compaction.test.ts +323 -0
- package/vendor/agent-root/src/agent/agent/__test__/concurrency.test.ts +290 -0
- package/vendor/agent-root/src/agent/agent/__test__/error-normalizer.test.ts +377 -0
- package/vendor/agent-root/src/agent/agent/__test__/error.test.ts +212 -0
- package/vendor/agent-root/src/agent/agent/__test__/fault-injection.test.ts +295 -0
- package/vendor/agent-root/src/agent/agent/__test__/index.test.ts +3607 -0
- package/vendor/agent-root/src/agent/agent/__test__/logger.test.ts +35 -0
- package/vendor/agent-root/src/agent/agent/__test__/message-utils.test.ts +517 -0
- package/vendor/agent-root/src/agent/agent/__test__/telemetry.test.ts +97 -0
- package/vendor/agent-root/src/agent/agent/__test__/timeout-budget.test.ts +479 -0
- package/vendor/agent-root/src/agent/agent/__test__/tool-call-merge.test.ts +80 -0
- package/vendor/agent-root/src/agent/agent/__test__/tool-execution-ledger.test.ts +76 -0
- package/vendor/agent-root/src/agent/agent/__test__/write-buffer.test.ts +173 -0
- package/vendor/agent-root/src/agent/agent/__test__/write-file-session.test.ts +109 -0
- package/vendor/agent-root/src/agent/agent/abort-runtime.ts +71 -0
- package/vendor/agent-root/src/agent/agent/callback-safety.ts +33 -0
- package/vendor/agent-root/src/agent/agent/compaction.ts +291 -0
- package/vendor/agent-root/src/agent/agent/concurrency.ts +103 -0
- package/vendor/agent-root/src/agent/agent/error-normalizer.ts +190 -0
- package/vendor/agent-root/src/agent/agent/error.ts +198 -0
- package/vendor/agent-root/src/agent/agent/index.ts +1772 -0
- package/vendor/agent-root/src/agent/agent/logger.ts +65 -0
- package/vendor/agent-root/src/agent/agent/message-utils.ts +101 -0
- package/vendor/agent-root/src/agent/agent/stream-events.ts +61 -0
- package/vendor/agent-root/src/agent/agent/telemetry.ts +123 -0
- package/vendor/agent-root/src/agent/agent/timeout-budget.ts +227 -0
- package/vendor/agent-root/src/agent/agent/tool-call-merge.ts +111 -0
- package/vendor/agent-root/src/agent/agent/tool-execution-ledger.ts +164 -0
- package/vendor/agent-root/src/agent/agent/write-buffer.ts +188 -0
- package/vendor/agent-root/src/agent/agent/write-file-session.ts +238 -0
- package/vendor/agent-root/src/agent/app/__test__/agent-app-service.test.ts +1053 -0
- package/vendor/agent-root/src/agent/app/__test__/minimal-agent-application.test.ts +158 -0
- package/vendor/agent-root/src/agent/app/__test__/sqlite-agent-app-store.test.ts +437 -0
- package/vendor/agent-root/src/agent/app/agent-app-service.ts +748 -0
- package/vendor/agent-root/src/agent/app/contracts.ts +109 -0
- package/vendor/agent-root/src/agent/app/index.ts +5 -0
- package/vendor/agent-root/src/agent/app/minimal-agent-application.ts +151 -0
- package/vendor/agent-root/src/agent/app/ports.ts +72 -0
- package/vendor/agent-root/src/agent/app/sqlite-agent-app-store.ts +1182 -0
- package/vendor/agent-root/src/agent/app/sqlite-client.ts +177 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/00-README.md +36 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/01-scope-and-goals.md +33 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/02-architecture-overview.md +40 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/03-domain-model-and-contracts.md +91 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/04-ports-and-interfaces.md +116 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/05-run-orchestration-and-state-machine.md +52 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/06-cli-commands-and-ux.md +53 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/07-storage-design-local.md +52 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/08-error-and-observability.md +40 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/09-security-and-policy-boundary.md +19 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/10-test-plan-and-acceptance.md +28 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/11-implementation-phases.md +26 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/12-open-questions-and-risks.md +30 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/13-sqlite-schema-fields-and-rationale.md +567 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/14-project-flow-mermaid.md +583 -0
- package/vendor/agent-root/src/agent/docs/cli-app-layer/15-openclaw-style-project-blueprint.md +972 -0
- package/vendor/agent-root/src/agent/error-contract.ts +154 -0
- package/vendor/agent-root/src/agent/prompts/system.ts +246 -0
- package/vendor/agent-root/src/agent/prompts/system1.ts +208 -0
- package/vendor/agent-root/src/agent/storage/__test__/file-history-store.test.ts +98 -0
- package/vendor/agent-root/src/agent/storage/file-history-store.ts +313 -0
- package/vendor/agent-root/src/agent/storage/file-storage-config.ts +94 -0
- package/vendor/agent-root/src/agent/storage/file-system.ts +31 -0
- package/vendor/agent-root/src/agent/storage/file-write-service.ts +21 -0
- package/vendor/agent-root/src/agent/tool/__test__/base-tool.test.ts +413 -0
- package/vendor/agent-root/src/agent/tool/__test__/bash-policy.test.ts +356 -0
- package/vendor/agent-root/src/agent/tool/__test__/bash.mocked-coverage.test.ts +375 -0
- package/vendor/agent-root/src/agent/tool/__test__/bash.test.ts +372 -0
- package/vendor/agent-root/src/agent/tool/__test__/error.test.ts +108 -0
- package/vendor/agent-root/src/agent/tool/__test__/file-edit-tool.test.ts +258 -0
- package/vendor/agent-root/src/agent/tool/__test__/file-history-tools.test.ts +121 -0
- package/vendor/agent-root/src/agent/tool/__test__/file-read-tool.test.ts +210 -0
- package/vendor/agent-root/src/agent/tool/__test__/glob.test.ts +139 -0
- package/vendor/agent-root/src/agent/tool/__test__/grep.mocked-coverage.test.ts +456 -0
- package/vendor/agent-root/src/agent/tool/__test__/grep.test.ts +192 -0
- package/vendor/agent-root/src/agent/tool/__test__/lsp.test.ts +300 -0
- package/vendor/agent-root/src/agent/tool/__test__/outside-workspace-confirmation.test.ts +214 -0
- package/vendor/agent-root/src/agent/tool/__test__/path-security.test.ts +336 -0
- package/vendor/agent-root/src/agent/tool/__test__/skill-loader.test.ts +494 -0
- package/vendor/agent-root/src/agent/tool/__test__/skill-parser.test.ts +543 -0
- package/vendor/agent-root/src/agent/tool/__test__/skill-tool.test.ts +172 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-concurrency-and-version.test.ts +116 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-create-get-list-update.test.ts +267 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-create.test.ts +519 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-errors.test.ts +225 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-output-blocking.test.ts +223 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-output.test.ts +184 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-parent-abort.test.ts +287 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-real-runner-adapter.test.ts +190 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-run-lifecycle.test.ts +352 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-store-runner-branches.test.ts +395 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-store.test.ts +391 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-subagent-config-integration.test.ts +176 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-subagent-config.test.ts +68 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-tools-core-edges.test.ts +630 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-tools-runtime-edges.test.ts +732 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-types.test.ts +494 -0
- package/vendor/agent-root/src/agent/tool/__test__/task-utils-branches.test.ts +175 -0
- package/vendor/agent-root/src/agent/tool/__test__/tool-manager.test.ts +505 -0
- package/vendor/agent-root/src/agent/tool/__test__/types.test.ts +55 -0
- package/vendor/agent-root/src/agent/tool/__test__/web-fetch.test.ts +244 -0
- package/vendor/agent-root/src/agent/tool/__test__/web-search.test.ts +290 -0
- package/vendor/agent-root/src/agent/tool/__test__/write-file.test.ts +368 -0
- package/vendor/agent-root/src/agent/tool/base-tool.ts +345 -0
- package/vendor/agent-root/src/agent/tool/bash-policy.ts +636 -0
- package/vendor/agent-root/src/agent/tool/bash.ts +688 -0
- package/vendor/agent-root/src/agent/tool/error.ts +131 -0
- package/vendor/agent-root/src/agent/tool/file-edit-tool.ts +264 -0
- package/vendor/agent-root/src/agent/tool/file-history-list.ts +103 -0
- package/vendor/agent-root/src/agent/tool/file-history-restore.ts +149 -0
- package/vendor/agent-root/src/agent/tool/file-read-tool.ts +211 -0
- package/vendor/agent-root/src/agent/tool/glob.ts +171 -0
- package/vendor/agent-root/src/agent/tool/grep.ts +496 -0
- package/vendor/agent-root/src/agent/tool/lsp.ts +481 -0
- package/vendor/agent-root/src/agent/tool/path-security.ts +117 -0
- package/vendor/agent-root/src/agent/tool/search/common.ts +153 -0
- package/vendor/agent-root/src/agent/tool/skill/index.ts +13 -0
- package/vendor/agent-root/src/agent/tool/skill/loader.ts +229 -0
- package/vendor/agent-root/src/agent/tool/skill/parser.ts +124 -0
- package/vendor/agent-root/src/agent/tool/skill/types.ts +27 -0
- package/vendor/agent-root/src/agent/tool/skill-tool.ts +143 -0
- package/vendor/agent-root/src/agent/tool/task-create.ts +186 -0
- package/vendor/agent-root/src/agent/tool/task-errors.ts +42 -0
- package/vendor/agent-root/src/agent/tool/task-get.ts +116 -0
- package/vendor/agent-root/src/agent/tool/task-graph.ts +78 -0
- package/vendor/agent-root/src/agent/tool/task-list.ts +141 -0
- package/vendor/agent-root/src/agent/tool/task-mock-runner-adapter.ts +232 -0
- package/vendor/agent-root/src/agent/tool/task-output.ts +223 -0
- package/vendor/agent-root/src/agent/tool/task-parent-abort.ts +115 -0
- package/vendor/agent-root/src/agent/tool/task-real-runner-adapter.ts +336 -0
- package/vendor/agent-root/src/agent/tool/task-runner-adapter.ts +55 -0
- package/vendor/agent-root/src/agent/tool/task-stop.ts +187 -0
- package/vendor/agent-root/src/agent/tool/task-store.ts +217 -0
- package/vendor/agent-root/src/agent/tool/task-subagent-config.ts +149 -0
- package/vendor/agent-root/src/agent/tool/task-types.ts +264 -0
- package/vendor/agent-root/src/agent/tool/task-update.ts +315 -0
- package/vendor/agent-root/src/agent/tool/task.ts +209 -0
- package/vendor/agent-root/src/agent/tool/tool-manager.ts +362 -0
- package/vendor/agent-root/src/agent/tool/tool-prompts.ts +242 -0
- package/vendor/agent-root/src/agent/tool/types.ts +116 -0
- package/vendor/agent-root/src/agent/tool/web-fetch.ts +227 -0
- package/vendor/agent-root/src/agent/tool/web-search.ts +208 -0
- package/vendor/agent-root/src/agent/tool/write-file.ts +497 -0
- package/vendor/agent-root/src/agent/types.ts +232 -0
- package/vendor/agent-root/src/agent/utils/__tests__/index.test.ts +18 -0
- package/vendor/agent-root/src/agent/utils/__tests__/message-utils.test.ts +610 -0
- package/vendor/agent-root/src/agent/utils/__tests__/message.test.ts +223 -0
- package/vendor/agent-root/src/agent/utils/__tests__/token.test.ts +42 -0
- package/vendor/agent-root/src/agent/utils/index.ts +16 -0
- package/vendor/agent-root/src/agent/utils/message.ts +171 -0
- package/vendor/agent-root/src/agent/utils/token.ts +28 -0
- package/vendor/agent-root/src/config/__tests__/load-config-to-env.test.ts +129 -0
- package/vendor/agent-root/src/config/__tests__/loader.test.ts +247 -0
- package/vendor/agent-root/src/config/__tests__/runtime.test.ts +88 -0
- package/vendor/agent-root/src/config/index.ts +54 -0
- package/vendor/agent-root/src/config/loader.ts +431 -0
- package/vendor/agent-root/src/config/paths.ts +30 -0
- package/vendor/agent-root/src/config/runtime.ts +163 -0
- package/vendor/agent-root/src/config/types.ts +70 -0
- package/vendor/agent-root/src/logger/index.ts +57 -0
- package/vendor/agent-root/src/logger/logger.ts +819 -0
- package/vendor/agent-root/src/logger/types.ts +150 -0
- package/vendor/agent-root/src/providers/__tests__/errors.test.ts +441 -0
- package/vendor/agent-root/src/providers/__tests__/index.test.ts +16 -0
- package/vendor/agent-root/src/providers/__tests__/openai-compatible.options.test.ts +318 -0
- package/vendor/agent-root/src/providers/__tests__/openai-compatible.test.ts +600 -0
- package/vendor/agent-root/src/providers/__tests__/registry.test.ts +449 -0
- package/vendor/agent-root/src/providers/__tests__/responses-adapter.test.ts +298 -0
- package/vendor/agent-root/src/providers/adapters/__tests__/anthropic.test.ts +354 -0
- package/vendor/agent-root/src/providers/adapters/__tests__/kimi.test.ts +58 -0
- package/vendor/agent-root/src/providers/adapters/__tests__/standard.test.ts +261 -0
- package/vendor/agent-root/src/providers/adapters/anthropic.ts +572 -0
- package/vendor/agent-root/src/providers/adapters/base.ts +131 -0
- package/vendor/agent-root/src/providers/adapters/kimi.ts +48 -0
- package/vendor/agent-root/src/providers/adapters/responses.ts +732 -0
- package/vendor/agent-root/src/providers/adapters/standard.ts +120 -0
- package/vendor/agent-root/src/providers/http/__tests__/client.timeout.test.ts +313 -0
- package/vendor/agent-root/src/providers/http/client.ts +289 -0
- package/vendor/agent-root/src/providers/http/stream-parser.ts +109 -0
- package/vendor/agent-root/src/providers/index.ts +76 -0
- package/vendor/agent-root/src/providers/kimi-headers.ts +177 -0
- package/vendor/agent-root/src/providers/openai-compatible.ts +387 -0
- package/vendor/agent-root/src/providers/registry/model-config.ts +230 -0
- package/vendor/agent-root/src/providers/registry/provider-factory.ts +123 -0
- package/vendor/agent-root/src/providers/registry.ts +135 -0
- package/vendor/agent-root/src/providers/types/api.ts +284 -0
- package/vendor/agent-root/src/providers/types/config.ts +58 -0
- package/vendor/agent-root/src/providers/types/errors.ts +323 -0
- package/vendor/agent-root/src/providers/types/index.ts +72 -0
- package/vendor/agent-root/src/providers/types/provider.ts +45 -0
- package/vendor/agent-root/src/providers/types/registry.ts +88 -0
|
@@ -0,0 +1,819 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Logger 核心实现
|
|
3
|
+
*
|
|
4
|
+
* 简洁实用的日志系统,支持:
|
|
5
|
+
* - 多级别日志 (TRACE ~ FATAL)
|
|
6
|
+
* - 多输出目标 (Console, File)
|
|
7
|
+
* - 结构化日志 (JSON/Pretty)
|
|
8
|
+
* - 日志轮转
|
|
9
|
+
* - 任意数据存储
|
|
10
|
+
* - 敏感字段脱敏
|
|
11
|
+
* - 异步上下文隔离
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import * as fs from 'node:fs';
|
|
15
|
+
import * as path from 'node:path';
|
|
16
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
17
|
+
import type {
|
|
18
|
+
LoggerConfig,
|
|
19
|
+
LogRecord,
|
|
20
|
+
LogLevel,
|
|
21
|
+
LogContext,
|
|
22
|
+
LogError,
|
|
23
|
+
LogMiddleware,
|
|
24
|
+
FileConfig,
|
|
25
|
+
ConsoleConfig,
|
|
26
|
+
ITransport,
|
|
27
|
+
LogCallback,
|
|
28
|
+
} from './types';
|
|
29
|
+
import { LogLevel as Lvl, LogLevelName } from './types';
|
|
30
|
+
|
|
31
|
+
// =============================================================================
|
|
32
|
+
// 默认配置
|
|
33
|
+
// =============================================================================
|
|
34
|
+
|
|
35
|
+
const DEFAULT_SENSITIVE_FIELDS = [
|
|
36
|
+
'apiKey',
|
|
37
|
+
'api_key',
|
|
38
|
+
'password',
|
|
39
|
+
'token',
|
|
40
|
+
'secret',
|
|
41
|
+
'authorization',
|
|
42
|
+
'credential',
|
|
43
|
+
];
|
|
44
|
+
|
|
45
|
+
const DEFAULT_CONFIG: Required<Omit<LoggerConfig, 'defaultContext'>> & {
|
|
46
|
+
defaultContext: LogContext;
|
|
47
|
+
} = {
|
|
48
|
+
service: 'app',
|
|
49
|
+
env: 'development',
|
|
50
|
+
level: Lvl.INFO,
|
|
51
|
+
defaultContext: {},
|
|
52
|
+
console: {
|
|
53
|
+
enabled: true,
|
|
54
|
+
format: 'pretty',
|
|
55
|
+
colorize: true,
|
|
56
|
+
},
|
|
57
|
+
file: {
|
|
58
|
+
enabled: false,
|
|
59
|
+
filepath: './logs/app.log',
|
|
60
|
+
format: 'json',
|
|
61
|
+
maxSize: 10 * 1024 * 1024, // 10MB
|
|
62
|
+
maxFiles: 5,
|
|
63
|
+
},
|
|
64
|
+
sensitiveFields: DEFAULT_SENSITIVE_FIELDS,
|
|
65
|
+
transports: [],
|
|
66
|
+
onLog: undefined as unknown as LogCallback,
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
// =============================================================================
|
|
70
|
+
// 工具函数
|
|
71
|
+
// =============================================================================
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* 安全序列化任意数据
|
|
75
|
+
*/
|
|
76
|
+
function safeStringify(data: unknown): string {
|
|
77
|
+
return stringifyWithCircular(data);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* 带循环引用检测的序列化
|
|
82
|
+
*/
|
|
83
|
+
function stringifyWithCircular(data: unknown, seen: WeakSet<object> = new WeakSet()): string {
|
|
84
|
+
if (data === null || data === undefined) {
|
|
85
|
+
return JSON.stringify(data);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (typeof data !== 'object') {
|
|
89
|
+
try {
|
|
90
|
+
return JSON.stringify(data);
|
|
91
|
+
} catch {
|
|
92
|
+
return JSON.stringify(String(data));
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// 处理特殊对象类型
|
|
97
|
+
if (data instanceof Error) {
|
|
98
|
+
return JSON.stringify({
|
|
99
|
+
__type: 'Error',
|
|
100
|
+
name: data.name,
|
|
101
|
+
message: data.message,
|
|
102
|
+
stack: data.stack,
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (data instanceof Date) {
|
|
107
|
+
return JSON.stringify(data.toISOString());
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (data instanceof Map) {
|
|
111
|
+
const obj: Record<string, unknown> = {};
|
|
112
|
+
for (const [k, v] of data) {
|
|
113
|
+
obj[String(k)] = v;
|
|
114
|
+
}
|
|
115
|
+
return stringifyWithCircular(obj, seen);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (data instanceof Set) {
|
|
119
|
+
return stringifyWithCircular(Array.from(data), seen);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (Buffer.isBuffer(data)) {
|
|
123
|
+
return JSON.stringify(data.toString('base64'));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (seen.has(data as object)) return '"[Circular]"';
|
|
127
|
+
seen.add(data as object);
|
|
128
|
+
|
|
129
|
+
if (Array.isArray(data)) {
|
|
130
|
+
const items = data.map((item) => stringifyWithCircular(item, seen));
|
|
131
|
+
return '[' + items.join(', ') + ']';
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const result: Record<string, unknown> = {};
|
|
135
|
+
for (const key of Object.keys(data as object)) {
|
|
136
|
+
const value = (data as Record<string, unknown>)[key];
|
|
137
|
+
if (typeof value === 'object' && value !== null) {
|
|
138
|
+
result[key] = JSON.parse(stringifyWithCircular(value, seen));
|
|
139
|
+
} else {
|
|
140
|
+
result[key] = value;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
try {
|
|
145
|
+
return JSON.stringify(result);
|
|
146
|
+
} catch {
|
|
147
|
+
return '"[Non-serializable]"';
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* 脱敏对象
|
|
153
|
+
*/
|
|
154
|
+
function sanitize(
|
|
155
|
+
obj: unknown,
|
|
156
|
+
sensitiveFields: string[],
|
|
157
|
+
seen: WeakSet<object> = new WeakSet()
|
|
158
|
+
): unknown {
|
|
159
|
+
if (!obj || typeof obj !== 'object') return obj;
|
|
160
|
+
if (seen.has(obj as object)) return '[Circular]';
|
|
161
|
+
seen.add(obj as object);
|
|
162
|
+
|
|
163
|
+
// 处理特殊类型
|
|
164
|
+
if (obj instanceof Error) {
|
|
165
|
+
return {
|
|
166
|
+
__type: 'Error',
|
|
167
|
+
name: obj.name,
|
|
168
|
+
message: obj.message,
|
|
169
|
+
stack: obj.stack,
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (obj instanceof Date) {
|
|
174
|
+
return obj.toISOString();
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (obj instanceof Map) {
|
|
178
|
+
const result: Record<string, unknown> = {};
|
|
179
|
+
for (const [k, v] of obj) {
|
|
180
|
+
result[String(k)] = sanitize(v, sensitiveFields, seen);
|
|
181
|
+
}
|
|
182
|
+
return result;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
if (obj instanceof Set) {
|
|
186
|
+
return Array.from(obj).map((item) => sanitize(item, sensitiveFields, seen));
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
if (Buffer.isBuffer(obj)) {
|
|
190
|
+
return obj.toString('base64');
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (Array.isArray(obj)) {
|
|
194
|
+
return obj.map((item) => sanitize(item, sensitiveFields, seen));
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const result: Record<string, unknown> = {};
|
|
198
|
+
for (const [key, value] of Object.entries(obj as Record<string, unknown>)) {
|
|
199
|
+
if (sensitiveFields.some((f) => key.toLowerCase().includes(f.toLowerCase()))) {
|
|
200
|
+
result[key] = '[REDACTED]';
|
|
201
|
+
} else if (typeof value === 'object' && value !== null) {
|
|
202
|
+
result[key] = sanitize(value, sensitiveFields, seen);
|
|
203
|
+
} else {
|
|
204
|
+
result[key] = value;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
return result;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// =============================================================================
|
|
211
|
+
// 格式化器
|
|
212
|
+
// =============================================================================
|
|
213
|
+
|
|
214
|
+
/** ANSI 颜色代码 */
|
|
215
|
+
const Colors = {
|
|
216
|
+
reset: '\x1b[0m',
|
|
217
|
+
bold: '\x1b[1m',
|
|
218
|
+
dim: '\x1b[2m',
|
|
219
|
+
red: '\x1b[31m',
|
|
220
|
+
green: '\x1b[32m',
|
|
221
|
+
yellow: '\x1b[33m',
|
|
222
|
+
cyan: '\x1b[36m',
|
|
223
|
+
bgRed: '\x1b[41m',
|
|
224
|
+
white: '\x1b[37m',
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
const LevelColors: Record<LogLevel, string> = {
|
|
228
|
+
[Lvl.TRACE]: Colors.dim,
|
|
229
|
+
[Lvl.DEBUG]: Colors.cyan,
|
|
230
|
+
[Lvl.INFO]: Colors.green,
|
|
231
|
+
[Lvl.WARN]: Colors.yellow,
|
|
232
|
+
[Lvl.ERROR]: Colors.red,
|
|
233
|
+
[Lvl.FATAL]: Colors.bgRed + Colors.white,
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* 格式化为 JSON
|
|
238
|
+
*/
|
|
239
|
+
function formatJson(record: LogRecord): string {
|
|
240
|
+
const output: Record<string, unknown> = {
|
|
241
|
+
'@timestamp': record.timestamp,
|
|
242
|
+
'@level': record.levelName,
|
|
243
|
+
'@message': record.message,
|
|
244
|
+
'@context': record.context, // 始终包含上下文
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
if (record.module) output['@module'] = record.module;
|
|
248
|
+
if (record.error) output['@error'] = record.error;
|
|
249
|
+
if (record.data !== undefined) output['@data'] = record.data;
|
|
250
|
+
|
|
251
|
+
return safeStringify(output);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* 格式化为可读文本
|
|
256
|
+
*/
|
|
257
|
+
function formatPretty(record: LogRecord, colorize: boolean): string {
|
|
258
|
+
const color = (text: string, code: string) => (colorize ? `${code}${text}${Colors.reset}` : text);
|
|
259
|
+
|
|
260
|
+
const parts: string[] = [];
|
|
261
|
+
|
|
262
|
+
// 时间戳
|
|
263
|
+
const date = new Date(record.timestamp);
|
|
264
|
+
const time =
|
|
265
|
+
date.toLocaleTimeString('zh-CN', {
|
|
266
|
+
hour: '2-digit',
|
|
267
|
+
minute: '2-digit',
|
|
268
|
+
second: '2-digit',
|
|
269
|
+
}) +
|
|
270
|
+
'.' +
|
|
271
|
+
String(date.getMilliseconds()).padStart(3, '0');
|
|
272
|
+
parts.push(color(time, Colors.dim));
|
|
273
|
+
|
|
274
|
+
// 级别
|
|
275
|
+
const levelName = LogLevelName[record.level].padEnd(5);
|
|
276
|
+
parts.push(color(levelName, LevelColors[record.level] + Colors.bold));
|
|
277
|
+
|
|
278
|
+
// 模块
|
|
279
|
+
if (record.module) {
|
|
280
|
+
parts.push(color(`[${record.module}]`, Colors.cyan));
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// 消息
|
|
284
|
+
parts.push(record.message);
|
|
285
|
+
|
|
286
|
+
// 上下文(最多5个字段)
|
|
287
|
+
if (Object.keys(record.context).length > 0) {
|
|
288
|
+
const pairs = Object.entries(record.context)
|
|
289
|
+
.slice(0, 5)
|
|
290
|
+
.map(([k, v]) => {
|
|
291
|
+
const str = typeof v === 'object' ? safeStringify(v) : String(v);
|
|
292
|
+
const truncated = str.length > 30 ? str.slice(0, 30) + '...' : str;
|
|
293
|
+
return `${k}=${truncated}`;
|
|
294
|
+
});
|
|
295
|
+
parts.push(color(`(${pairs.join(', ')})`, Colors.dim));
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
let output = parts.join(' ');
|
|
299
|
+
|
|
300
|
+
// 错误信息
|
|
301
|
+
if (record.error) {
|
|
302
|
+
output += '\n' + formatErrorDisplay(record.error, colorize);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// 数据
|
|
306
|
+
if (record.data !== undefined) {
|
|
307
|
+
output += '\n' + color(safeStringify(record.data), Colors.dim);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
return output;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
function formatErrorDisplay(error: LogError, colorize: boolean): string {
|
|
314
|
+
const color = (text: string, code: string) => (colorize ? `${code}${text}${Colors.reset}` : text);
|
|
315
|
+
const lines: string[] = [];
|
|
316
|
+
|
|
317
|
+
lines.push(color('┌─ Error ─────────────────────────────', Colors.red));
|
|
318
|
+
lines.push(color(`│ Type: ${error.name}`, Colors.red));
|
|
319
|
+
lines.push(color(`│ Message: ${error.message}`, Colors.red));
|
|
320
|
+
if (error.code) lines.push(color(`│ Code: ${error.code}`, Colors.red));
|
|
321
|
+
if (error.stack) {
|
|
322
|
+
lines.push(color('│ Stack:', Colors.red));
|
|
323
|
+
for (const line of error.stack.split('\n').slice(0, 8)) {
|
|
324
|
+
lines.push(color(`│ ${line}`, Colors.dim));
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
lines.push(color('└──────────────────────────────────────', Colors.red));
|
|
328
|
+
|
|
329
|
+
return lines.join('\n');
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// =============================================================================
|
|
333
|
+
// 上下文管理器
|
|
334
|
+
// =============================================================================
|
|
335
|
+
|
|
336
|
+
class ContextManager {
|
|
337
|
+
private static instance: ContextManager;
|
|
338
|
+
private readonly storage = new AsyncLocalStorage<LogContext>();
|
|
339
|
+
private fallback: LogContext = {};
|
|
340
|
+
|
|
341
|
+
static getInstance(): ContextManager {
|
|
342
|
+
if (!ContextManager.instance) {
|
|
343
|
+
ContextManager.instance = new ContextManager();
|
|
344
|
+
}
|
|
345
|
+
return ContextManager.instance;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
get(): LogContext {
|
|
349
|
+
return this.storage.getStore() ?? this.fallback;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
set(context: LogContext): void {
|
|
353
|
+
this.fallback = context;
|
|
354
|
+
this.storage.enterWith(context);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
update(context: Partial<LogContext>): void {
|
|
358
|
+
const next = { ...this.get(), ...context };
|
|
359
|
+
this.fallback = next;
|
|
360
|
+
this.storage.enterWith(next);
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
clear(): void {
|
|
364
|
+
this.fallback = {};
|
|
365
|
+
this.storage.enterWith({});
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
run<T>(context: LogContext, fn: () => T): T {
|
|
369
|
+
return this.storage.run({ ...this.get(), ...context }, fn);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
export const contextManager = ContextManager.getInstance();
|
|
374
|
+
|
|
375
|
+
// =============================================================================
|
|
376
|
+
// Transport 实现
|
|
377
|
+
// =============================================================================
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* 控制台输出
|
|
381
|
+
*/
|
|
382
|
+
class ConsoleTransport implements ITransport {
|
|
383
|
+
readonly name = 'console';
|
|
384
|
+
private config: {
|
|
385
|
+
enabled: boolean;
|
|
386
|
+
format: 'json' | 'pretty';
|
|
387
|
+
colorize: boolean;
|
|
388
|
+
level?: LogLevel;
|
|
389
|
+
};
|
|
390
|
+
|
|
391
|
+
constructor(config: ConsoleConfig) {
|
|
392
|
+
this.config = {
|
|
393
|
+
format: 'pretty',
|
|
394
|
+
colorize: true,
|
|
395
|
+
...config,
|
|
396
|
+
enabled: config.enabled ?? true,
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
write(record: LogRecord, globalLevel: LogLevel): void {
|
|
401
|
+
if (!this.config.enabled) return;
|
|
402
|
+
if (record.level < (this.config.level ?? globalLevel)) return;
|
|
403
|
+
|
|
404
|
+
const formatted =
|
|
405
|
+
this.config.format === 'json'
|
|
406
|
+
? formatJson(record)
|
|
407
|
+
: formatPretty(record, this.config.colorize);
|
|
408
|
+
|
|
409
|
+
// ERROR/FATAL 输出到 stderr
|
|
410
|
+
const stream = record.level >= Lvl.ERROR ? process.stderr : process.stdout;
|
|
411
|
+
stream.write(formatted + '\n');
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* 文件输出(支持日志轮转)
|
|
417
|
+
*/
|
|
418
|
+
class FileTransport implements ITransport {
|
|
419
|
+
readonly name = 'file';
|
|
420
|
+
private config: {
|
|
421
|
+
enabled: boolean;
|
|
422
|
+
filepath: string;
|
|
423
|
+
format: 'json' | 'pretty';
|
|
424
|
+
level?: LogLevel;
|
|
425
|
+
maxSize: number;
|
|
426
|
+
maxFiles: number;
|
|
427
|
+
};
|
|
428
|
+
private stream: fs.WriteStream | null = null;
|
|
429
|
+
private currentSize = 0;
|
|
430
|
+
|
|
431
|
+
constructor(config: FileConfig) {
|
|
432
|
+
this.config = {
|
|
433
|
+
format: 'json',
|
|
434
|
+
maxSize: 10 * 1024 * 1024,
|
|
435
|
+
maxFiles: 5,
|
|
436
|
+
...config,
|
|
437
|
+
enabled: config.enabled ?? false,
|
|
438
|
+
filepath: config.filepath ?? './logs/app.log',
|
|
439
|
+
};
|
|
440
|
+
|
|
441
|
+
if (this.config.enabled) {
|
|
442
|
+
this.initStream();
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
private initStream(): void {
|
|
447
|
+
const dir = path.dirname(this.config.filepath);
|
|
448
|
+
if (!fs.existsSync(dir)) {
|
|
449
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
this.stream = fs.createWriteStream(this.config.filepath, { flags: 'a', encoding: 'utf8' });
|
|
453
|
+
|
|
454
|
+
try {
|
|
455
|
+
this.currentSize = fs.statSync(this.config.filepath).size;
|
|
456
|
+
} catch {
|
|
457
|
+
this.currentSize = 0;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
this.stream.on('error', (err) => {
|
|
461
|
+
console.error(`[Logger] File transport error: ${err.message}`);
|
|
462
|
+
});
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
write(record: LogRecord, globalLevel: LogLevel): void {
|
|
466
|
+
if (!this.config.enabled || !this.stream) return;
|
|
467
|
+
if (record.level < (this.config.level ?? globalLevel)) return;
|
|
468
|
+
|
|
469
|
+
const formatted =
|
|
470
|
+
this.config.format === 'pretty' ? formatPretty(record, false) : formatJson(record);
|
|
471
|
+
|
|
472
|
+
const content = formatted + '\n';
|
|
473
|
+
this.stream.write(content);
|
|
474
|
+
this.currentSize += Buffer.byteLength(content, 'utf8');
|
|
475
|
+
|
|
476
|
+
this.checkRotation();
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
private checkRotation(): void {
|
|
480
|
+
if (this.currentSize < this.config.maxSize) return;
|
|
481
|
+
|
|
482
|
+
// 关闭当前流
|
|
483
|
+
if (this.stream) {
|
|
484
|
+
this.stream.end();
|
|
485
|
+
this.stream = null;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
const dir = path.dirname(this.config.filepath);
|
|
489
|
+
const ext = path.extname(this.config.filepath);
|
|
490
|
+
const base = path.basename(this.config.filepath, ext);
|
|
491
|
+
|
|
492
|
+
// 轮转文件
|
|
493
|
+
for (let i = this.config.maxFiles - 1; i >= 1; i--) {
|
|
494
|
+
const oldFile = path.join(dir, `${base}.${i}${ext}`);
|
|
495
|
+
const newFile = path.join(dir, `${base}.${i + 1}${ext}`);
|
|
496
|
+
try {
|
|
497
|
+
if (fs.existsSync(oldFile)) {
|
|
498
|
+
if (i === this.config.maxFiles - 1) {
|
|
499
|
+
fs.unlinkSync(oldFile);
|
|
500
|
+
} else {
|
|
501
|
+
fs.renameSync(oldFile, newFile);
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
} catch (err) {
|
|
505
|
+
console.error(`[Logger] Rotation error: ${(err as Error).message}`);
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
// 重命名当前文件
|
|
510
|
+
try {
|
|
511
|
+
fs.renameSync(this.config.filepath, path.join(dir, `${base}.1${ext}`));
|
|
512
|
+
} catch (err) {
|
|
513
|
+
console.error(`[Logger] Rename error: ${(err as Error).message}`);
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
this.initStream();
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
close(): void {
|
|
520
|
+
if (this.stream) {
|
|
521
|
+
this.stream.end();
|
|
522
|
+
this.stream = null;
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
// =============================================================================
|
|
528
|
+
// Logger 类
|
|
529
|
+
// =============================================================================
|
|
530
|
+
|
|
531
|
+
/**
|
|
532
|
+
* 日志器类
|
|
533
|
+
*/
|
|
534
|
+
export class Logger {
|
|
535
|
+
private config: Required<Omit<LoggerConfig, 'defaultContext' | 'transports' | 'onLog'>> & {
|
|
536
|
+
defaultContext: LogContext;
|
|
537
|
+
transports?: ITransport[];
|
|
538
|
+
onLog?: LogCallback;
|
|
539
|
+
};
|
|
540
|
+
private middlewares: LogMiddleware[] = [];
|
|
541
|
+
private consoleTransport: ConsoleTransport;
|
|
542
|
+
private fileTransport: FileTransport;
|
|
543
|
+
private customTransports: ITransport[] = [];
|
|
544
|
+
private closed = false;
|
|
545
|
+
|
|
546
|
+
constructor(config?: Partial<LoggerConfig>) {
|
|
547
|
+
this.config = {
|
|
548
|
+
...DEFAULT_CONFIG,
|
|
549
|
+
...config,
|
|
550
|
+
console: { ...DEFAULT_CONFIG.console, ...config?.console },
|
|
551
|
+
file: { ...DEFAULT_CONFIG.file, ...config?.file },
|
|
552
|
+
defaultContext: config?.defaultContext ?? {},
|
|
553
|
+
sensitiveFields: config?.sensitiveFields ?? DEFAULT_CONFIG.sensitiveFields,
|
|
554
|
+
transports: config?.transports,
|
|
555
|
+
onLog: config?.onLog,
|
|
556
|
+
};
|
|
557
|
+
|
|
558
|
+
this.consoleTransport = new ConsoleTransport(this.config.console);
|
|
559
|
+
this.fileTransport = new FileTransport(this.config.file);
|
|
560
|
+
|
|
561
|
+
this.customTransports = config?.transports ?? [];
|
|
562
|
+
|
|
563
|
+
// 默认上下文中间件
|
|
564
|
+
if (Object.keys(this.config.defaultContext).length > 0) {
|
|
565
|
+
this.middlewares.push((record, next) => {
|
|
566
|
+
record.context = { ...this.config.defaultContext, ...record.context };
|
|
567
|
+
next();
|
|
568
|
+
});
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
// 敏感字段脱敏中间件
|
|
572
|
+
if (this.config.sensitiveFields.length > 0) {
|
|
573
|
+
this.middlewares.push((record, next) => {
|
|
574
|
+
record.context = sanitize(record.context, this.config.sensitiveFields) as LogContext;
|
|
575
|
+
if (record.data !== undefined) {
|
|
576
|
+
record.data = sanitize(record.data, this.config.sensitiveFields);
|
|
577
|
+
}
|
|
578
|
+
next();
|
|
579
|
+
});
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
/**
|
|
584
|
+
* 添加中间件
|
|
585
|
+
*/
|
|
586
|
+
use(middleware: LogMiddleware): this {
|
|
587
|
+
this.middlewares.push(middleware);
|
|
588
|
+
return this;
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
/**
|
|
592
|
+
* 添加自定义 Transport
|
|
593
|
+
*/
|
|
594
|
+
addTransport(transport: ITransport): this {
|
|
595
|
+
this.customTransports.push(transport);
|
|
596
|
+
return this;
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
/**
|
|
600
|
+
* 创建日志记录
|
|
601
|
+
*/
|
|
602
|
+
private createRecord(
|
|
603
|
+
level: LogLevel,
|
|
604
|
+
message: string,
|
|
605
|
+
context?: LogContext,
|
|
606
|
+
error?: Error,
|
|
607
|
+
data?: unknown
|
|
608
|
+
): LogRecord {
|
|
609
|
+
const globalContext = contextManager.get();
|
|
610
|
+
const record: LogRecord = {
|
|
611
|
+
timestamp: new Date().toISOString(),
|
|
612
|
+
level,
|
|
613
|
+
levelName: LogLevelName[level],
|
|
614
|
+
message,
|
|
615
|
+
context: { ...globalContext, ...context },
|
|
616
|
+
data,
|
|
617
|
+
};
|
|
618
|
+
|
|
619
|
+
if (error) {
|
|
620
|
+
record.error = {
|
|
621
|
+
name: error.name,
|
|
622
|
+
message: error.message,
|
|
623
|
+
stack: error.stack,
|
|
624
|
+
};
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
return record;
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
/**
|
|
631
|
+
* 执行中间件链
|
|
632
|
+
*/
|
|
633
|
+
private executeMiddlewares(record: LogRecord): void {
|
|
634
|
+
let index = 0;
|
|
635
|
+
const next = () => {
|
|
636
|
+
if (index < this.middlewares.length) {
|
|
637
|
+
this.middlewares[index++](record, next);
|
|
638
|
+
}
|
|
639
|
+
};
|
|
640
|
+
next();
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
/**
|
|
644
|
+
* 写入日志
|
|
645
|
+
*/
|
|
646
|
+
private log(record: LogRecord): void {
|
|
647
|
+
if (this.closed) return;
|
|
648
|
+
if (record.level < this.config.level) return;
|
|
649
|
+
|
|
650
|
+
this.executeMiddlewares(record);
|
|
651
|
+
|
|
652
|
+
// 写入内置 Transport
|
|
653
|
+
this.consoleTransport.write(record, this.config.level);
|
|
654
|
+
this.fileTransport.write(record, this.config.level);
|
|
655
|
+
|
|
656
|
+
// 写入自定义 Transport
|
|
657
|
+
for (const transport of this.customTransports) {
|
|
658
|
+
try {
|
|
659
|
+
transport.write(record, this.config.level);
|
|
660
|
+
} catch (err) {
|
|
661
|
+
console.error(`[Logger] Transport "${transport.name ?? 'unknown'}" error:`, err);
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
// 调用 onLog 回调
|
|
666
|
+
if (this.config.onLog) {
|
|
667
|
+
try {
|
|
668
|
+
this.config.onLog(record);
|
|
669
|
+
} catch (err) {
|
|
670
|
+
console.error('[Logger] onLog callback error:', err);
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
// ---------------------------------------------------------------------------
|
|
676
|
+
// 日志方法
|
|
677
|
+
// ---------------------------------------------------------------------------
|
|
678
|
+
|
|
679
|
+
trace(message: string, context?: LogContext): void {
|
|
680
|
+
this.log(this.createRecord(Lvl.TRACE, message, context));
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
debug(message: string, context?: LogContext, data?: unknown): void {
|
|
684
|
+
this.log(this.createRecord(Lvl.DEBUG, message, context, undefined, data));
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
info(message: string, context?: LogContext, data?: unknown): void {
|
|
688
|
+
this.log(this.createRecord(Lvl.INFO, message, context, undefined, data));
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
warn(message: string, context?: LogContext, data?: unknown): void {
|
|
692
|
+
this.log(this.createRecord(Lvl.WARN, message, context, undefined, data));
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
error(message: string, error?: Error | unknown, context?: LogContext): void {
|
|
696
|
+
const err = error instanceof Error ? error : undefined;
|
|
697
|
+
const data = error !== undefined && !(error instanceof Error) ? error : undefined;
|
|
698
|
+
this.log(this.createRecord(Lvl.ERROR, message, context, err, data));
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
fatal(message: string, error?: Error | unknown, context?: LogContext): void {
|
|
702
|
+
const err = error instanceof Error ? error : undefined;
|
|
703
|
+
const data = error !== undefined && !(error instanceof Error) ? error : undefined;
|
|
704
|
+
this.log(this.createRecord(Lvl.FATAL, message, context, err, data));
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
/**
|
|
708
|
+
* 直接记录 LogRecord
|
|
709
|
+
*/
|
|
710
|
+
write(record: LogRecord): void {
|
|
711
|
+
this.log(record);
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
// ---------------------------------------------------------------------------
|
|
715
|
+
// 子日志器
|
|
716
|
+
// ---------------------------------------------------------------------------
|
|
717
|
+
|
|
718
|
+
child(module: string, additionalContext?: LogContext): ChildLogger {
|
|
719
|
+
return new ChildLogger(this, module, additionalContext);
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
// ---------------------------------------------------------------------------
|
|
723
|
+
// 生命周期
|
|
724
|
+
// ---------------------------------------------------------------------------
|
|
725
|
+
|
|
726
|
+
close(): void {
|
|
727
|
+
if (this.closed) return;
|
|
728
|
+
this.closed = true;
|
|
729
|
+
this.fileTransport.close();
|
|
730
|
+
|
|
731
|
+
// 关闭自定义 Transport
|
|
732
|
+
for (const transport of this.customTransports) {
|
|
733
|
+
if (transport.close) {
|
|
734
|
+
try {
|
|
735
|
+
transport.close();
|
|
736
|
+
} catch (err) {
|
|
737
|
+
console.error(`[Logger] Transport "${transport.name ?? 'unknown'}" close error:`, err);
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
getConfig(): Readonly<typeof this.config> {
|
|
744
|
+
// 返回浅拷贝,避免外部修改内部配置
|
|
745
|
+
return {
|
|
746
|
+
...this.config,
|
|
747
|
+
console: { ...this.config.console },
|
|
748
|
+
file: { ...this.config.file },
|
|
749
|
+
sensitiveFields: [...this.config.sensitiveFields],
|
|
750
|
+
};
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
/**
|
|
755
|
+
* 子日志器
|
|
756
|
+
*/
|
|
757
|
+
export class ChildLogger {
|
|
758
|
+
constructor(
|
|
759
|
+
private parent: Logger,
|
|
760
|
+
private module: string,
|
|
761
|
+
private context: LogContext = {}
|
|
762
|
+
) {}
|
|
763
|
+
|
|
764
|
+
private merge(ctx?: LogContext): LogContext {
|
|
765
|
+
return { ...this.context, ...ctx };
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
trace(message: string, context?: LogContext): void {
|
|
769
|
+
this.parent.trace(`[${this.module}] ${message}`, this.merge(context));
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
debug(message: string, context?: LogContext, data?: unknown): void {
|
|
773
|
+
this.parent.debug(`[${this.module}] ${message}`, this.merge(context), data);
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
info(message: string, context?: LogContext, data?: unknown): void {
|
|
777
|
+
this.parent.info(`[${this.module}] ${message}`, this.merge(context), data);
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
warn(message: string, context?: LogContext, data?: unknown): void {
|
|
781
|
+
this.parent.warn(`[${this.module}] ${message}`, this.merge(context), data);
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
error(message: string, error?: Error | unknown, context?: LogContext): void {
|
|
785
|
+
this.parent.error(`[${this.module}] ${message}`, error, this.merge(context));
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
fatal(message: string, error?: Error | unknown, context?: LogContext): void {
|
|
789
|
+
this.parent.fatal(`[${this.module}] ${message}`, error, this.merge(context));
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
child(subModule: string, additionalContext?: LogContext): ChildLogger {
|
|
793
|
+
return new ChildLogger(this.parent, `${this.module}:${subModule}`, {
|
|
794
|
+
...this.context,
|
|
795
|
+
...additionalContext,
|
|
796
|
+
});
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
// =============================================================================
|
|
801
|
+
// 全局实例
|
|
802
|
+
// =============================================================================
|
|
803
|
+
|
|
804
|
+
let defaultLogger: Logger | null = null;
|
|
805
|
+
|
|
806
|
+
export function getLogger(): Logger {
|
|
807
|
+
if (!defaultLogger) {
|
|
808
|
+
defaultLogger = new Logger();
|
|
809
|
+
}
|
|
810
|
+
return defaultLogger;
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
export function setDefaultLogger(logger: Logger): void {
|
|
814
|
+
defaultLogger = logger;
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
export function createLogger(config?: Partial<LoggerConfig>): Logger {
|
|
818
|
+
return new Logger(config);
|
|
819
|
+
}
|