..
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 | import chalk from 'chalk';
type LogLevel = 'info' | 'warn' | 'error' | 'debug' | 'success';
interface LogOptions {
timestamp?: boolean;
prefix?: string;
}
class Logger {
private static writeStream = process.stdout;
private static errorStream = process.stderr;
private static getTimestamp(): string {
return new Date().toISOString();
}
private static formatMessage(message: string, level: LogLevel, options?: LogOptions): string {
const parts: string[] = [];
if (options?.timestamp) {
parts.push(chalk.gray(this.getTimestamp()));
}
const levelColor = {
info: chalk.blue('INFO'),
warn: chalk.yellow('WARN'),
error: chalk.red('ERROR'),
debug: chalk.magenta('DEBUG'),
success: chalk.green('SUCCESS')
};
parts.push(levelColor[level]);
if (options?.prefix) {
parts.push(chalk.cyan(`[${options.prefix}]`));
}
const messageColor = {
info: chalk.white,
warn: chalk.yellow,
error: chalk.red,
debug: chalk.gray,
success: chalk.green
};
parts.push(messageColor[level](message));
return parts.join(' ') + '\n';
}
private static write(message: string, level: LogLevel, options?: LogOptions): void {
const formattedMessage = this.formatMessage(message, level, options);
if (level === 'error' || level === 'warn') {
this.errorStream.write(formattedMessage);
} else {
this.writeStream.write(formattedMessage);
}
}
static info(message: string, options?: LogOptions): void {
this.write(message, 'info', options);
}
static warn(message: string, options?: LogOptions): void {
this.write(message, 'warn', options);
}
static error(message: string | Error, options?: LogOptions): void {
const errorMessage = message instanceof Error ? message.stack || message.message : message;
this.write(errorMessage, 'error', options);
}
static debug(message: string, options?: LogOptions): void {
if (process.env.NODE_ENV !== 'production') {
this.write(message, 'debug', options);
}
}
static success(message: string, options?: LogOptions): void {
this.write(message, 'success', options);
}
}
export default Logger;
|
|