-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlogger.ts
More file actions
75 lines (60 loc) · 1.54 KB
/
Copy pathlogger.ts
File metadata and controls
75 lines (60 loc) · 1.54 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
import chalk from 'chalk';
import { DEBUG_LOG_TITLE } from './constant.js';
enum LogType {
Stdout = 'stdout',
Stderr = 'stderr',
}
const logMap: Record<LogType, 'log' | 'error'> = {
[LogType.Stdout]: 'log',
[LogType.Stderr]: 'error',
};
export class Logger {
stdout: string;
stderr: string;
name: string;
logTitle: string;
constructor({
name,
}: {
name: string;
}) {
this.name = name;
this.stdout = '';
this.stderr = '';
this.logTitle = DEBUG_LOG_TITLE;
}
appendLog(type: 'stdout' | 'stderr', log: string) {
this[type] += log;
}
emitLog(type: 'stdout' | 'stderr') {
console[logMap[type]](this[type]);
}
emitLogOnce(type: 'stdout' | 'stderr', log: string) {
console[logMap[type]](log);
}
reset(type: 'stdout' | 'stderr') {
this[type] = '';
}
setBanner(name: string) {
const startBanner = `\n------------ ${chalk.hex('#c95ab3').bold('log start:')} ${chalk.green(
name,
)} ${'-'.repeat(Math.max(50 - name.toString().length, 5))}`;
this.stdout = startBanner + this.stdout;
}
flushStdout() {
this.setBanner(this.name);
this.emitLog(LogType.Stdout);
}
static setEndBanner() {
const endBanner = `------------ ${chalk.hex('#c95ab3').bold('log end:')} ${chalk.green(
'all sub project have been started.',
)} ------------\n`;
console.log(endBanner);
}
}
export const debugLog = (msg: string) => {
const isDebug = process.env.DEBUG === 'rsbuild' || process.env.DEBUG === '*';
if (isDebug) {
console.log(DEBUG_LOG_TITLE + msg);
}
};