type LogSource = string;
export enum LogLevel { None = -1, Error = 0, Warn = 1, Log = 2, Info = 3, Debug = 4, }export const DEFAULT_LOG_LEVEL = LogLevel.Error;
type LogLevels = Record<LogSource, LogLevel>;
let globalLogLevels: LogLevels = { _default: DEFAULT_LOG_LEVEL,};
export function updateLogLevels(newLogLevels: LogLevels): void { globalLogLevels = { ...globalLogLevels, ...newLogLevels, };}
export function setLogLevel(source: LogSource, level: LogLevel) { globalLogLevels[source] = level;}
export function setDefaultLogLevel(level: LogLevel) { globalLogLevels._default = level;}
export function getLogLevel(source: LogSource): LogLevel { if (source in globalLogLevels) { return globalLogLevels[source]; } else { return globalLogLevels._default; }}
export function getLogLevels(): LogLevels { return globalLogLevels;}
type LogColor = | "blue" | "aqua" | "darkcyan" | "cyan" | "dimgray" | "slategray" | "green" | "springgreen" | "grey" | "darkMagenta" | "magenta" | "red" | "orangeRed" | "salmon" | "lightsalmon" | "gold" | "yellow";
export class Logger { source: LogSource; color: LogColor | undefined = undefined;
constructor(source: LogSource, color?: LogColor) { this.source = source; this.color = color || "aqua"; }
_print(level: LogLevel, showTag: boolean, indent: string, ...args: any[]) { if (level <= getLogLevel(this.source)) { if (showTag) { const tag = `[${this.source}]`;
if (this.color !== undefined) { const tagArgs = [`%c ${tag}`, `color: ${this.color}`]; console.log(indent, ...tagArgs, ...args); } else { console.log(indent, tag, ...args); } } else { console.log(indent, ...args); } } }
error(...args: any[]) { this._print(LogLevel.Error, true, "!!", ...args); } warn(...args: any[]) { this._print(LogLevel.Warn, true, "! ", ...args); } log(...args: any[]) { this._print(LogLevel.Log, true, " ", ...args); } info(...args: any[]) { this._print(LogLevel.Info, true, " ", ...args); } debug(...args: any[]) { this._print(LogLevel.Debug, true, " ", ...args); }
blank() { this._print(LogLevel.Info, false, ""); }}