-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.js
42 lines (34 loc) · 1.48 KB
/
logger.js
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
import chalk from 'chalk';
const logger = {
log: (level, message, value = '') => {
const now = new Date().toLocaleString();
const colors = {
info: chalk.green,
warn: chalk.yellow,
error: chalk.red,
success: chalk.blue,
debug: chalk.magenta,
};
const color = colors[level] || chalk.white;
const levelTag = `[ ${level.toUpperCase()} ]`;
const timestamp = `[ ${now} ]`;
const formattedMessage = `${chalk.green("[ OpenLedger ]")} ${chalk.cyanBright(timestamp)} ${color(levelTag)} ${message}`;
let formattedValue = ` ${chalk.green(value)}`;
if (level === 'error') {
formattedValue = ` ${chalk.red(value)}`;
} else if (level === 'warn') {
formattedValue = ` ${chalk.yellow(value)}`;
}
if (typeof value === 'object') {
const valueColor = level === 'error' ? chalk.red : chalk.green;
formattedValue = ` ${valueColor(JSON.stringify(value))}`;
}
console.log(`${formattedMessage}${formattedValue}`);
},
info: (message, value = '') => logger.log('info', message, value),
warn: (message, value = '') => logger.log('warn', message, value),
error: (message, value = '') => logger.log('error', message, value),
success: (message, value = '') => logger.log('success', message, value),
debug: (message, value = '') => logger.log('debug', message, value),
};
export default logger;