| 1 | import chalk from "chalk"; |
| 2 | |
| 3 | // Track if we're in headless mode (default to false) |
| 4 | let isHeadlessMode = false; |
| 5 | |
| 6 | /** |
| 7 | * Configure the logger to enable/disable headless mode |
| 8 | */ |
| 9 | export function configureLogger(options: { headless: boolean }) { |
| 10 | isHeadlessMode = options.headless; |
| 11 | } |
| 12 | |
| 13 | /** |
| 14 | * Check if logging is currently enabled |
| 15 | * This will return false if in headless mode unless specifically exempted |
| 16 | */ |
| 17 | export function isLoggingEnabled(forceEnable = false): boolean { |
| 18 | return !isHeadlessMode || forceEnable; |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Wrapper for console.info that respects headless mode |
| 23 | * If forceLog is true, it will log even in headless mode |
| 24 | */ |
| 25 | export function log(message: any, forceLog = false): void { |
| 26 | if (isLoggingEnabled(forceLog)) { |
| 27 | console.info(message); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Wrapper for console.error that respects headless mode |
| 33 | * Errors are generally important, so we default forceLog to true |
| 34 | */ |
| 35 | export function error(message: any, forceLog = true): void { |
| 36 | if (isLoggingEnabled(forceLog)) { |
| 37 | console.error(message); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Wrapper for console.info that respects headless mode |
| 43 | */ |
| 44 | export function info(message: any, forceLog = false): void { |
| 45 | if (isLoggingEnabled(forceLog)) { |
| 46 | console.info(message); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Wrapper for console.warn that respects headless mode |
| 52 | */ |
| 53 | export function warn(message: any, forceLog = false): void { |
| 54 | if (isLoggingEnabled(forceLog)) { |
| 55 | console.warn(message); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Colored logging helpers that respect headless mode |
| 61 | */ |
| 62 | export const loggers = { |
| 63 | success: (message: string, forceLog = false) => |
| 64 | log(chalk.green(message), forceLog), |
| 65 | info: (message: string, forceLog = false) => |
| 66 | log(chalk.blue(message), forceLog), |
| 67 | warning: (message: string, forceLog = false) => |
| 68 | log(chalk.yellow(message), forceLog), |
| 69 | error: (message: string, forceLog = true) => |
| 70 | error(chalk.red(message), forceLog), |
| 71 | debug: (message: string, forceLog = false) => |
| 72 | log(chalk.gray(message), forceLog), |
| 73 | command: (message: string) => log(message, true), // Always show command outputs |
| 74 | }; |