-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
logger.ts
180 lines (148 loc) · 5.79 KB
/
logger.ts
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import chalk from 'chalk';
import figlet from 'figlet';
import moment from 'moment';
import { PassThrough } from 'stream';
import { WriteStream } from 'tty';
import Constants from '../constants.js';
import LogLevel from './logLevel.js';
import ProgressBar, { ProgressBarSymbol } from './progressBar.js';
import ProgressBarCLI from './progressBarCLI.js';
/**
* {@link Logger} is a class that deals with the formatting and outputting log messages to a stream.
*/
export default class Logger {
private logLevel: LogLevel;
private readonly stream: NodeJS.WritableStream;
private readonly loggerPrefix?: string;
constructor(
logLevel: LogLevel = LogLevel.WARN,
stream: NodeJS.WritableStream = process.stdout,
loggerPrefix?: string,
) {
this.logLevel = logLevel;
this.stream = stream;
this.loggerPrefix = loggerPrefix;
}
getLogLevel(): LogLevel {
return this.logLevel;
}
setLogLevel(logLevel: LogLevel): void {
this.logLevel = logLevel;
}
getStream(): NodeJS.WritableStream {
return this.stream;
}
/**
* Determine if this {@link Logger}'s underlying stream is a TTY stream or not.
*/
isTTY(): boolean {
if (this.stream instanceof WriteStream) {
return (this.stream satisfies WriteStream).isTTY;
}
if (this.stream instanceof PassThrough) {
// Testing streams should be treated as TTY
return true;
}
return false;
}
private readonly print = (logLevel: LogLevel, message: unknown = ''): void => {
if (this.logLevel > logLevel) {
return;
}
this.stream.write(`${this.formatMessage(logLevel, String(message).toString())}\n`);
};
/**
* Print a newline.
*/
newLine(): void {
this.print(LogLevel.ALWAYS);
}
/**
* Format a log message for a given {@link LogLevel}.
*/
formatMessage(logLevel: LogLevel, message: string): string {
// Don't format "ALWAYS" or "NEVER"
if (logLevel >= LogLevel.ALWAYS) {
return message;
}
const chalkFuncs = {
[LogLevel.TRACE]: chalk.grey,
[LogLevel.DEBUG]: chalk.magenta,
[LogLevel.INFO]: chalk.cyan,
[LogLevel.WARN]: chalk.yellow,
[LogLevel.ERROR]: chalk.red,
[LogLevel.NOTICE]: chalk.underline,
[LogLevel.ALWAYS]: (msg) => msg,
[LogLevel.NEVER]: (msg) => msg,
} satisfies { [key in LogLevel]: (message: string) => string };
const chalkFunc = chalkFuncs[logLevel];
const loggerTime = this.logLevel <= LogLevel.TRACE ? `[${moment().format('HH:mm:ss.SSS')}] ` : '';
const levelPrefix = `${chalkFunc(LogLevel[logLevel])}:${' '.repeat(Math.max(5 - LogLevel[logLevel].length, 0))} `;
const loggerPrefix = this.logLevel <= LogLevel.INFO && this.loggerPrefix ? `${this.loggerPrefix}: ` : '';
return message
.replace(/Error: /, '') // strip `new Error()` prefix
.replace(/(\r?\n)(\r?\n)+/, '$1')
.split('\n')
.map((m) => (m.trim() ? loggerTime + levelPrefix + loggerPrefix + m : m))
.join('\n');
}
trace = (message: unknown = ''): void => this.print(LogLevel.TRACE, message);
debug = (message: unknown = ''): void => this.print(LogLevel.DEBUG, message);
info = (message: unknown = ''): void => this.print(LogLevel.INFO, message);
warn = (message: unknown = ''): void => this.print(LogLevel.WARN, message);
error = (message: unknown = ''): void => this.print(LogLevel.ERROR, message);
notice = (message: unknown = ''): void => this.print(LogLevel.NOTICE, message);
/**
* Print the CLI header.
*/
printHeader(): void {
const logo = figlet.textSync(Constants.COMMAND_NAME.toUpperCase(), {
font: 'Big Money-se',
}).trimEnd();
const logoSplit = logo.split('\n');
const midLine = Math.min(Math.ceil(logoSplit.length / 2), logoSplit.length - 1);
const maxLineLen = logoSplit.reduce((max, line) => Math.max(max, line.length), 0);
logoSplit[midLine - 2] = `${logoSplit[midLine - 1].padEnd(maxLineLen, ' ')} ROM collection manager`;
logoSplit[midLine - 1] = `${logoSplit[midLine - 1].padEnd(maxLineLen, ' ')} ${Constants.HOMEPAGE}`;
logoSplit[midLine + 1] = `${logoSplit[midLine + 1].padEnd(maxLineLen, ' ')} v${Constants.COMMAND_VERSION}`;
this.print(LogLevel.ALWAYS, `${logoSplit.join('\n')}\n\n`);
}
/**
* Print a colorized yargs help string.
*/
colorizeYargs(help: string): void {
this.print(
LogLevel.ALWAYS,
help
.replace(/^(Usage:.+)/, chalk.bold('$1'))
.replace(/(\[commands\.*\])/g, chalk.magenta('$1'))
.replace(new RegExp(`(${Constants.COMMAND_NAME}) (( ?[a-z])+)`, 'g'), `$1 ${chalk.magenta('$2')}`)
.replace(/(\[options\.*\])/g, chalk.cyan('$1'))
.replace(/([^a-zA-Z0-9-])(-[a-zA-Z0-9]+)/g, `$1${chalk.cyanBright('$2')}`)
.replace(/(--[a-zA-Z0-9][a-zA-Z0-9-]+(\n[ \t]+)?[a-zA-Z0-9-]+) ((?:[^ -])[^"][^ \n]*|"(?:[^"\\]|\\.)*")/g, `$1 ${chalk.underline('$3')}`)
.replace(/(--[a-zA-Z0-9][a-zA-Z0-9-]+(\n[ \t]+)?[a-zA-Z0-9-]+)/g, chalk.cyan('$1'))
.replace(/(<[a-zA-Z]+>)/g, chalk.blue('$1'))
.replace(/(\[(array|boolean|count|number|string)\])/g, chalk.grey('$1'))
.replace(/(\[default: ([^[\]]+(\[[^\]]+\])?)*\])/g, chalk.green('$1'))
.replace(/(\[required\])/g, chalk.red('$1'))
.replace(/(\{[a-zA-Z]+\})/g, chalk.yellow('$1'))
.replace(new RegExp(` (${Constants.COMMAND_NAME}) `, 'g'), ` ${chalk.blueBright('$1')} `),
);
}
/**
* Create a {@link ProgressBar} with a reference to this {@link Logger}.
*/
async addProgressBar(
name: string,
symbol = ProgressBarSymbol.WAITING,
initialTotal = 0,
): Promise<ProgressBar> {
return ProgressBarCLI.new(this, name, symbol, initialTotal);
}
/**
* Return a copy of this Logger with a new string prefix.
*/
withLoggerPrefix(prefix: string): Logger {
return new Logger(this.logLevel, this.stream, prefix);
}
}