logger.ts
3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import chalk from 'chalk';
/**
* Log levels emitted by the logger.
*/
export type LogLevel = 'log' | 'info' | 'warn' | 'error' | 'success' | 'test';
/**
* A single log entry forwarded to subscribers (used by the service mode SSE stream).
*/
export interface LogEntry {
level: LogLevel;
message: string;
timestamp: string;
}
type LogListener = (entry: LogEntry) => void;
// Subscribers (e.g. the web service) receive every log entry in real time.
const listeners = new Set<LogListener>();
/**
* Subscribe to all log output. Returns an unsubscribe function.
* Used by the service mode to stream console logs to the web UI via SSE.
*/
export function subscribeLogs(listener: LogListener): () => void {
listeners.add(listener);
return () => {
listeners.delete(listener);
};
}
/**
* Get current timestamp in YYYY-MM-DD HH:mm:ss format
*/
function getTimestamp(): string {
const now = new Date();
const year = now.getFullYear();
const month = String(now.getMonth() + 1).padStart(2, '0');
const day = String(now.getDate()).padStart(2, '0');
const hours = String(now.getHours()).padStart(2, '0');
const minutes = String(now.getMinutes()).padStart(2, '0');
const seconds = String(now.getSeconds()).padStart(2, '0');
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
/**
* Forward an entry to all subscribers (best-effort, never throws).
*/
function emit(level: LogLevel, message: string): void {
if (listeners.size === 0) return;
const entry: LogEntry = { level, message, timestamp: getTimestamp() };
for (const listener of listeners) {
try {
listener(entry);
} catch {
// Ignore subscriber errors to avoid breaking the main flow
}
}
}
/**
* Logger utility for consistent console output with colors
*/
export const logger = {
/**
* Print standard log (alias for info logs).
*
* Many subsystems (e.g. validation) use `printLog()` as the default logging method.
*/
printLog(message: string): void {
console.log(message);
emit('log', message);
},
/**
* Print info log in blue
*/
printInfoLog(message: string): void {
console.log(chalk.blue(`[${getTimestamp()}] [INFO] ${message}`));
emit('info', message);
},
/**
* Print warning log in yellow
*/
printWarnLog(message: string): void {
console.warn(chalk.yellow(`[${getTimestamp()}] [WARNING] ${message}`));
emit('warn', message);
},
/**
* Print error log in red
*/
printErrorLog(message: string): void {
console.error(chalk.red(`[${getTimestamp()}] [ERROR] ✖ ${message}`));
emit('error', message);
},
/**
* Print test/debug log in gray
* Only logs in development/test environments
*/
printTestLog(message: string): void {
if (process.env.NODE_ENV === 'development') {
console.log(chalk.gray(`[${getTimestamp()}] [DEBUG] ${message}`));
emit('test', message);
}
},
/**
* Print success log in green
*/
printSuccessLog(message: string): void {
console.log(chalk.green(`[${getTimestamp()}] [SUCCESS] ✔ ${message}`));
emit('success', message);
},
/**
* 向编排后端报告当前生成阶段与进度百分比。
* 输出纯文本标记(无 ANSI、无时间戳),由后端 runner 解析以驱动前端进度条。
*/
reportPhase(phase: string, percent: number): void {
process.stdout.write(`__D2C_PHASE__:${phase}:${Math.round(percent)}\n`);
},
};