-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathlogger.ts
59 lines (50 loc) · 1.53 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
import chalk from 'chalk';
import * as nodeConsole from 'console';
import redent from 'redent';
import * as nodeUtil from 'util';
import { getConfig } from '../config';
export const logger = {
debug(message: unknown, ...args: unknown[]) {
const output = formatMessage('●', message, ...args);
nodeConsole.debug(chalk.dim(output));
},
info(message: unknown, ...args: unknown[]) {
const output = formatMessage('●', message, ...args);
nodeConsole.info(output);
},
warn(message: unknown, ...args: unknown[]) {
const output = formatMessage('▲', message, ...args);
nodeConsole.warn(chalk.yellow(output));
},
error(message: unknown, ...args: unknown[]) {
const output = formatMessage('■', message, ...args);
nodeConsole.error(chalk.red(output));
},
};
export const debugLogger = {
debug(message: unknown, ...args: unknown[]) {
if (getConfig().debug) {
logger.debug(message, ...args);
}
},
info(message: unknown, ...args: unknown[]) {
if (getConfig().debug) {
logger.info(message, ...args);
}
},
warn(message: unknown, ...args: unknown[]) {
if (getConfig().debug) {
logger.warn(message, ...args);
}
},
error(message: unknown, ...args: unknown[]) {
if (getConfig().debug) {
logger.error(message, ...args);
}
},
};
function formatMessage(symbol: string, message: unknown, ...args: unknown[]) {
const formatted = nodeUtil.format(message, ...args);
const indented = redent(formatted, 4);
return ` ${symbol} ${indented.trimStart()}\n`;
}