all repos — stealth-developers @ 9e70b9738062e72ae29c11654064b6f755567753

src/utils/logging.ts (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
const COLOURS = {
	RESET: "\x1b[0m",
	BRIGHT: "\x1b[1m",
	DIM: "\x1b[2m",
	UNDERSCORE: "\x1b[4m",
	BLINK: "\x1b[5m",
	REVERSE: "\x1b[7m",
	HIDDEN: "\x1b[8m",

	FG_BLACK: "\x1b[30m",
	FG_RED: "\x1b[31m",
	FG_GREEN: "\x1b[32m",
	FG_YELLOW: "\x1b[33m",
	FG_BLUE: "\x1b[34m",
	FG_MAGENTA: "\x1b[35m",
	FG_CYAN: "\x1b[36m",
	FG_WHITE: "\x1b[37m",

	BG_BLACK: "\x1b[40m",
	BG_RED: "\x1b[41m",
	BG_GREEN: "\x1b[42m",
	BG_YELLOW: "\x1b[43m",
	BG_BLUE: "\x1b[44m",
	BG_MAGENTA: "\x1b[45m",
	BG_CYAN: "\x1b[46m",
	BG_WHITE: "\x1b[47m",
};

enum LogLevel {
	DEBUG = 0,
	INFO = 1,
	WARN = 2,
	ERROR = 3,
	FATAL = 4,
	OFF = 5,
}

interface LogOptions {
	timestamp?: boolean;
	colorize?: boolean;
	scopeColor?: string;
}

class Logger {
	private static logLevel: LogLevel = LogLevel.INFO;
	private scope: string[];
	private options: LogOptions;
	private static readonly MAX_LEVEL_LENGTH = Math.max(
		...Object.keys(LogLevel)
			.filter((k) => Number.isNaN(Number(k)))
			.map((k) => k.length),
	);

	private static readonly SCOPE_COLOURS = [
		COLOURS.FG_RED,
		COLOURS.FG_GREEN,
		COLOURS.FG_YELLOW,
		COLOURS.FG_BLUE,
		COLOURS.FG_MAGENTA,
		COLOURS.FG_CYAN,
	];

	private static scopeColours: { [scopeName: string]: string } = {};

	constructor(scope: string | string[] = [], options: LogOptions = {}) {
		this.scope = Array.isArray(scope) ? scope : [scope];
		this.options = {
			timestamp: true,
			colorize: true,
			scopeColor: this.getScopeColor(this.scope.join("/")),
			...options,
		};

		if (process.env.LOG_LEVEL) {
			try {
				const envLogLevel = process.env.LOG_LEVEL.toUpperCase();
				if (envLogLevel in LogLevel) {
					Logger.setLogLevel(LogLevel[envLogLevel as keyof typeof LogLevel]);
				} else {
					console.warn(
						`Invalid LOG_LEVEL environment variable: ${process.env.LOG_LEVEL}. Using default: INFO.`,
					);
				}
			} catch (e) {
				console.warn(`Error parsing LOG_LEVEL: ${e}. Using default: INFO.`);
			}
		}
	}

	static setLogLevel(level: LogLevel): void {
		Logger.logLevel = level;
	}

	private getLevelColor(level: LogLevel): string {
		switch (level) {
			case LogLevel.DEBUG:
				return COLOURS.FG_WHITE;
			case LogLevel.INFO:
				return COLOURS.FG_GREEN;
			case LogLevel.WARN:
				return COLOURS.FG_YELLOW;
			case LogLevel.ERROR:
				return COLOURS.FG_RED;
			case LogLevel.FATAL:
				return COLOURS.BG_RED + COLOURS.FG_WHITE;
			default:
				return COLOURS.RESET;
		}
	}

	private getScopeColor(scopeName: string): string {
		if (Logger.scopeColours[scopeName]) {
			return Logger.scopeColours[scopeName];
		}

		const colourIndex =
			Object.keys(Logger.scopeColours).length % Logger.SCOPE_COLOURS.length;
		const colour = Logger.SCOPE_COLOURS[colourIndex];
		Logger.scopeColours[scopeName] = colour;
		return colour;
	}

	private formatMessage(level: LogLevel, message: string): string {
		const now = new Date();
		const timestamp = this.options.timestamp
			? `${COLOURS.DIM}[${now.toLocaleString()}]${COLOURS.RESET} `
			: "";
		const levelString = LogLevel[level];
		const scopeString =
			this.scope.length > 0 ? `[${this.scope.join("/")}] ` : "";
		const levelColor = this.getLevelColor(level);
		const COLOURStart = this.options.colorize ? levelColor : "";
		const colorEnd = this.options.colorize ? COLOURS.RESET : "";

		const formattedMessage = `${timestamp}${COLOURStart}[${levelString}]${colorEnd} ${this.options.colorize && this.options.scopeColor ? this.options.scopeColor : ""}${scopeString}${COLOURS.RESET}${message}`;
		return formattedMessage;
	}

	private log(level: LogLevel, message: string, ...args: unknown[]): void {
		if (level >= Logger.logLevel) {
			const formattedMessage = this.formatMessage(level, message);
			console.log(formattedMessage, ...args);
		}
	}

	debug(message: string, ...args: unknown[]): void {
		this.log(LogLevel.DEBUG, message, ...args);
	}

	info(message: string, ...args: unknown[]): void {
		this.log(LogLevel.INFO, message, ...args);
	}

	warn(message: string, ...args: unknown[]): void {
		this.log(LogLevel.WARN, message, ...args);
	}

	error(message: string, ...args: unknown[]): void {
		this.log(LogLevel.ERROR, message, ...args);
	}

	fatal(message: string, ...args: unknown[]): void {
		this.log(LogLevel.FATAL, message, ...args);
	}

	newLine(): void {
		console.log();
	}

	child(scope: string | string[], options: LogOptions = {}): Logger {
		const newScope = Array.isArray(scope)
			? [...this.scope, ...scope]
			: [...this.scope, scope];
		return new Logger(newScope, { ...this.options, ...options });
	}
}

const singleton = new Logger("global", {
	timestamp: true,
	colorize: true,
	scopeColor: COLOURS.FG_MAGENTA,
});

export default singleton;
export { Logger, LogLevel, COLOURS };