|
| 1 | +import { getContext } from '../context/async-context'; |
| 2 | +import colors from '../utils/colors'; |
| 3 | +import { ILogger } from './ILogger'; |
| 4 | + |
| 5 | +enum LogLevel { |
| 6 | + DEBUG, |
| 7 | + INFO, |
| 8 | + WARN, |
| 9 | + ERROR, |
| 10 | + DEFAULT, |
| 11 | +} |
| 12 | + |
| 13 | +const execHandlerKind = { |
| 14 | + autocomplete: 'Autocomplete', |
| 15 | + messageContextMenu: 'MessageContextMenu', |
| 16 | + userContextMenu: 'UserContextMenu', |
| 17 | + chatInput: 'SlashCommand', |
| 18 | + message: 'Message', |
| 19 | +}; |
| 20 | + |
| 21 | +const commandHandlerType = { |
| 22 | + legacy: 'Legacy', |
| 23 | + app: 'App', |
| 24 | +}; |
| 25 | + |
| 26 | +export class DefaultLogger implements ILogger { |
| 27 | + private logger: Console; |
| 28 | + |
| 29 | + public constructor( |
| 30 | + public stdout = process.stdout, |
| 31 | + public stderr = process.stderr, |
| 32 | + ) { |
| 33 | + this.logger = new console.Console(this.stdout, this.stderr); |
| 34 | + } |
| 35 | + |
| 36 | + private _getContext(): string { |
| 37 | + const ctx = getContext(); |
| 38 | + if (!ctx) return ''; |
| 39 | + |
| 40 | + const kind = ctx.variables.get('execHandlerKind'); |
| 41 | + const commandHandler = ctx.variables.get('commandHandlerType'); |
| 42 | + const command = ctx.variables.get('currentCommandName'); |
| 43 | + if (!kind && !commandHandler) return ''; |
| 44 | + |
| 45 | + const h = execHandlerKind[kind as keyof typeof execHandlerKind]; |
| 46 | + |
| 47 | + if (!h && !commandHandler) return ''; |
| 48 | + |
| 49 | + const t = commandHandler === 'app' ? '(app ✨) ' : ''; |
| 50 | + |
| 51 | + return `${colors.green(`${t}${command} ${h ? colors.gray(h) : ''}`.trim())}`; |
| 52 | + } |
| 53 | + |
| 54 | + private _getPrefix(level: LogLevel): string { |
| 55 | + const timestamp = new Date().toISOString(); |
| 56 | + const ctx = this._getContext(); |
| 57 | + |
| 58 | + switch (level) { |
| 59 | + case LogLevel.DEBUG: |
| 60 | + return `${colors.bgBlue(colors.black('[DEBUG]'))} | ${colors.gray(timestamp)} | ${ctx} - `; |
| 61 | + case LogLevel.INFO: |
| 62 | + return `${colors.bgGreen(colors.black('[INFO]'))} | ${colors.gray(timestamp)} | ${ctx} - `; |
| 63 | + case LogLevel.WARN: |
| 64 | + return `${colors.bgYellow(colors.black('[WARN]'))} | ${colors.gray(timestamp)} | ${ctx} - `; |
| 65 | + case LogLevel.ERROR: |
| 66 | + return `${colors.bgRed(colors.black('[ERROR]'))} | ${colors.gray(timestamp)} | ${ctx} - `; |
| 67 | + default: |
| 68 | + return `${colors.bgWhite(colors.black('[LOG]'))} | ${colors.gray(timestamp)} | ${ctx} - `; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private _log(level: LogLevel, ...args: any[]): void { |
| 73 | + const prefix = this._getPrefix(level); |
| 74 | + this.logger.log(prefix, ...args); |
| 75 | + } |
| 76 | + |
| 77 | + public debug(...args: any[]): void { |
| 78 | + this._log(LogLevel.DEBUG, ...args); |
| 79 | + } |
| 80 | + |
| 81 | + public error(...args: any[]): void { |
| 82 | + this._log(LogLevel.ERROR, ...args); |
| 83 | + } |
| 84 | + |
| 85 | + public log(...args: any[]): void { |
| 86 | + this._log(LogLevel.DEFAULT, ...args); |
| 87 | + } |
| 88 | + |
| 89 | + public info(...args: any[]): void { |
| 90 | + this._log(LogLevel.INFO, ...args); |
| 91 | + } |
| 92 | + |
| 93 | + public warn(...args: any[]): void { |
| 94 | + this._log(LogLevel.WARN, ...args); |
| 95 | + } |
| 96 | +} |
0 commit comments