utils/src/logger.ts

109 lines
2.9 KiB
TypeScript
Raw Normal View History

2024-02-28 19:08:51 -05:00
import {TypedEmitter, TypedEvents} from './emitter';
2024-10-16 19:55:35 -04:00
import {JSONSanitize} from './objects.ts';
2024-02-28 19:08:51 -05:00
2024-02-07 01:33:07 -05:00
export const CliEffects = {
CLEAR: "\x1b[0m",
BRIGHT: "\x1b[1m",
DIM: "\x1b[2m",
UNDERSCORE: "\x1b[4m",
BLINK: "\x1b[5m",
REVERSE: "\x1b[7m",
HIDDEN: "\x1b[8m",
}
export const CliForeground = {
2024-02-28 19:08:51 -05:00
BLACK: '\x1b[30m',
RED: '\x1b[31m',
GREEN: '\x1b[32m',
YELLOW: '\x1b[33m',
BLUE: '\x1b[34m',
MAGENTA: '\x1b[35m',
CYAN: '\x1b[36m',
LIGHT_GREY: '\x1b[37m',
GREY: '\x1b[90m',
LIGHT_RED: '\x1b[91m',
LIGHT_GREEN: '\x1b[92m',
LIGHT_YELLOW: '\x1b[93m',
LIGHT_BLUE: '\x1b[94m',
LIGHT_MAGENTA: '\x1b[95m',
LIGHT_CYAN: '\x1b[96m',
WHITE: '\x1b[97m',
2024-02-07 01:33:07 -05:00
}
export const CliBackground = {
BLACK: "\x1b[40m",
RED: "\x1b[41m",
GREEN: "\x1b[42m",
YELLOW: "\x1b[43m",
BLUE: "\x1b[44m",
MAGENTA: "\x1b[45m",
CYAN: "\x1b[46m",
WHITE: "\x1b[47m",
GREY: "\x1b[100m",
}
2024-02-28 19:08:51 -05:00
export enum LOG_LEVEL {
2024-04-11 20:22:44 -04:00
ERROR = 0,
WARN = 1,
2024-03-04 11:11:11 -05:00
INFO = 2,
2024-04-11 20:22:44 -04:00
LOG = 3,
DEBUG = 4,
2024-02-28 19:08:51 -05:00
}
export type LoggerEvents = TypedEvents & {
'ERROR': (...args: any[]) => any;
2024-04-11 20:22:44 -04:00
'WARN': (...args: any[]) => any;
'INFO': (...args: any[]) => any;
'LOG': (...args: any[]) => any;
'DEBUG': (...args: any[]) => any;
2024-02-28 19:08:51 -05:00
};
export class Logger extends TypedEmitter<LoggerEvents> {
2024-04-11 20:22:44 -04:00
static LOG_LEVEL: LOG_LEVEL = LOG_LEVEL.DEBUG;
2024-02-28 19:08:51 -05:00
constructor(public readonly namespace?: string) {
2024-02-28 19:08:51 -05:00
super();
}
2024-02-07 01:33:07 -05:00
2024-10-16 20:32:41 -04:00
protected format(...text: any[]): string {
const now = new Date();
2024-10-16 20:32:41 -04:00
const timestamp = `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()} ${now.getHours().toString().padStart(2, '0')}:${now.getMinutes().toString().padStart(2, '0')}:${now.getSeconds().toString().padStart(2, '0')}.${now.getMilliseconds().toString().padEnd(3, '0')}`;
2024-10-16 20:28:56 -04:00
return `${timestamp}${this.namespace ? ` [${this.namespace}]` : ''} ${text.map(t => typeof t == 'string' ? t : JSONSanitize(t, 2)).join(' ')}`;
2024-02-07 01:33:07 -05:00
}
2024-10-16 20:21:35 -04:00
debug(...args: any[]) {
2024-04-11 20:22:44 -04:00
if(Logger.LOG_LEVEL < LOG_LEVEL.DEBUG) return;
2024-03-04 11:11:11 -05:00
const str = this.format(...args);
2024-04-11 20:22:44 -04:00
Logger.emit(LOG_LEVEL.DEBUG, str);
2024-03-04 11:11:11 -05:00
console.debug(CliForeground.LIGHT_GREY + str + CliEffects.CLEAR);
2024-02-07 01:33:07 -05:00
}
2024-10-16 20:21:35 -04:00
log(...args: any[]) {
2024-04-11 20:22:44 -04:00
if(Logger.LOG_LEVEL < LOG_LEVEL.LOG) return;
2024-03-04 11:11:11 -05:00
const str = this.format(...args);
2024-04-11 20:22:44 -04:00
Logger.emit(LOG_LEVEL.LOG, str);
2024-03-04 11:11:11 -05:00
console.log(CliEffects.CLEAR + str);
2024-02-07 01:33:07 -05:00
}
2024-10-16 20:21:35 -04:00
info(...args: any[]) {
2024-04-11 20:22:44 -04:00
if(Logger.LOG_LEVEL < LOG_LEVEL.INFO) return;
2024-03-04 11:11:11 -05:00
const str = this.format(...args);
Logger.emit(LOG_LEVEL.INFO, str);
console.info(CliForeground.BLUE + str + CliEffects.CLEAR);
2024-02-07 01:33:07 -05:00
}
2024-10-16 20:21:35 -04:00
warn(...args: any[]) {
2024-04-11 20:22:44 -04:00
if(Logger.LOG_LEVEL < LOG_LEVEL.WARN) return;
2024-03-04 11:11:11 -05:00
const str = this.format(...args);
Logger.emit(LOG_LEVEL.WARN, str);
console.warn(CliForeground.YELLOW + str + CliEffects.CLEAR);
2024-02-07 01:33:07 -05:00
}
2024-10-16 20:21:35 -04:00
error(...args: any[]) {
2024-04-11 20:22:44 -04:00
if(Logger.LOG_LEVEL < LOG_LEVEL.ERROR) return;
2024-03-04 11:11:11 -05:00
const str = this.format(...args);
Logger.emit(LOG_LEVEL.ERROR, str);
console.error(CliForeground.RED + str + CliEffects.CLEAR);
2024-02-07 01:33:07 -05:00
}
}