|
| 1 | +import chalk from 'chalk'; |
| 2 | + |
| 3 | +const orange = '#D24E01'; |
1 | 4 | let verboseEnabled = false; |
2 | 5 |
|
3 | 6 | /** |
4 | | - * Log verbose message in verbose logging is enabled |
5 | | - * @param args |
| 7 | + * Log a message |
| 8 | + * @param args The arguments to log |
| 9 | + */ |
| 10 | +function log(...args: any[]) { |
| 11 | + args = args.map((arg) => { |
| 12 | + if (typeof arg === 'string') { |
| 13 | + // Regular expression to find text within square brackets |
| 14 | + return arg.replace(/\[(.*?)\]/g, (match) => chalk.gray(match)); // Colorizes the entire bracketed content |
| 15 | + } |
| 16 | + return arg; |
| 17 | + }); |
| 18 | + console.log(...args); |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * Log an important message |
| 23 | + * @param args The arguments to log |
| 24 | + */ |
| 25 | +function important(...args: any[]) { |
| 26 | + console.log(chalk.hex(orange)(...args)); |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Log an error message in red |
| 31 | + * @param args The arguments to log |
| 32 | + */ |
| 33 | +function error(...args: any[]) { |
| 34 | + console.error(chalk.red(...args)); |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Log a warning message in orange |
| 39 | + * @param args The arguments to log |
| 40 | + */ |
| 41 | +function warn(...args: any[]) { |
| 42 | + console.warn(chalk.hex(orange)(...args)); |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Log a verbose message if verbose is enabled. Log the message in grey. |
| 47 | + * @param args The arguments to log |
6 | 48 | */ |
7 | 49 | function verbose(...args: any[]) { |
8 | 50 | if (verboseEnabled) { |
9 | | - console.info(...args); |
| 51 | + console.info(chalk.grey(...args)); |
10 | 52 | } |
11 | 53 | } |
12 | 54 |
|
13 | 55 | /** |
14 | | - * |
15 | | - * @param enabled |
| 56 | + * Set the verbosity of logging |
| 57 | + * @param enabled Whether verbose logging should be enabled |
16 | 58 | */ |
17 | 59 | function setVerbose(enabled: boolean) { |
18 | 60 | verboseEnabled = enabled; |
19 | 61 | } |
20 | 62 |
|
21 | 63 | export const Logger = { |
22 | | - log: console.log, |
23 | | - error: console.error, |
24 | | - warn: console.warn, |
| 64 | + log, |
| 65 | + error, |
| 66 | + warn, |
| 67 | + important, |
25 | 68 | verbose, |
26 | 69 | setVerbose, |
27 | 70 | }; |
0 commit comments