125 lines
2.9 KiB
TypeScript
125 lines
2.9 KiB
TypeScript
|
|
/**
|
|||
|
|
* Logging utilities for Onebox
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
type LogLevel = 'info' | 'success' | 'warn' | 'error' | 'debug';
|
|||
|
|
|
|||
|
|
class Logger {
|
|||
|
|
private debugMode = false;
|
|||
|
|
|
|||
|
|
constructor() {
|
|||
|
|
this.debugMode = Deno.args.includes('--debug') || Deno.env.get('DEBUG') === 'true';
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* Log a message with specified level
|
|||
|
|
*/
|
|||
|
|
log(level: LogLevel, message: string, ...args: unknown[]): void {
|
|||
|
|
const timestamp = new Date().toISOString();
|
|||
|
|
const prefix = this.getPrefix(level);
|
|||
|
|
|
|||
|
|
const formattedMessage = `${prefix} ${message}`;
|
|||
|
|
|
|||
|
|
switch (level) {
|
|||
|
|
case 'error':
|
|||
|
|
console.error(formattedMessage, ...args);
|
|||
|
|
break;
|
|||
|
|
case 'warn':
|
|||
|
|
console.warn(formattedMessage, ...args);
|
|||
|
|
break;
|
|||
|
|
case 'debug':
|
|||
|
|
if (this.debugMode) {
|
|||
|
|
console.log(formattedMessage, ...args);
|
|||
|
|
}
|
|||
|
|
break;
|
|||
|
|
default:
|
|||
|
|
console.log(formattedMessage, ...args);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* Info level logging
|
|||
|
|
*/
|
|||
|
|
info(message: string, ...args: unknown[]): void {
|
|||
|
|
this.log('info', message, ...args);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* Success level logging
|
|||
|
|
*/
|
|||
|
|
success(message: string, ...args: unknown[]): void {
|
|||
|
|
this.log('success', message, ...args);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* Warning level logging
|
|||
|
|
*/
|
|||
|
|
warn(message: string, ...args: unknown[]): void {
|
|||
|
|
this.log('warn', message, ...args);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* Error level logging
|
|||
|
|
*/
|
|||
|
|
error(message: string, ...args: unknown[]): void {
|
|||
|
|
this.log('error', message, ...args);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* Debug level logging (only when --debug flag is present)
|
|||
|
|
*/
|
|||
|
|
debug(message: string, ...args: unknown[]): void {
|
|||
|
|
this.log('debug', message, ...args);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* Get colored prefix for log level
|
|||
|
|
*/
|
|||
|
|
private getPrefix(level: LogLevel): string {
|
|||
|
|
const colors = {
|
|||
|
|
info: '\x1b[36m', // Cyan
|
|||
|
|
success: '\x1b[32m', // Green
|
|||
|
|
warn: '\x1b[33m', // Yellow
|
|||
|
|
error: '\x1b[31m', // Red
|
|||
|
|
debug: '\x1b[90m', // Gray
|
|||
|
|
};
|
|||
|
|
const reset = '\x1b[0m';
|
|||
|
|
|
|||
|
|
const icons = {
|
|||
|
|
info: 'ℹ',
|
|||
|
|
success: '✓',
|
|||
|
|
warn: '⚠',
|
|||
|
|
error: '✖',
|
|||
|
|
debug: '⚙',
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
return `${colors[level]}${icons[level]}${reset}`;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* Print a table (simplified version)
|
|||
|
|
*/
|
|||
|
|
table(headers: string[], rows: string[][]): void {
|
|||
|
|
// Calculate column widths
|
|||
|
|
const widths = headers.map((header, i) => {
|
|||
|
|
const maxContentWidth = Math.max(
|
|||
|
|
...rows.map((row) => (row[i] || '').toString().length)
|
|||
|
|
);
|
|||
|
|
return Math.max(header.length, maxContentWidth);
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
// Print header
|
|||
|
|
const headerRow = headers.map((h, i) => h.padEnd(widths[i])).join(' ');
|
|||
|
|
console.log(headerRow);
|
|||
|
|
console.log(headers.map((_, i) => '-'.repeat(widths[i])).join(' '));
|
|||
|
|
|
|||
|
|
// Print rows
|
|||
|
|
for (const row of rows) {
|
|||
|
|
const formattedRow = row.map((cell, i) => (cell || '').toString().padEnd(widths[i])).join(' ');
|
|||
|
|
console.log(formattedRow);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
export const logger = new Logger();
|