Clean up logger class

This commit is contained in:
powermaker450 2025-01-24 19:21:04 -05:00
parent 9c734b5248
commit fa4758da38

View file

@ -16,78 +16,55 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import chalk from "chalk"; import { green, yellow, red, bold } from "chalk";
import { LOG_LEVEL } from "./config"; import { LOG_LEVEL } from "./config";
export default class Logger { export default class Logger {
private _main: string; private main: string;
public readonly _loglevel = +LOG_LEVEL;
public readonly name: string; public readonly name: string;
private readonly isMain: boolean; private readonly isMain: boolean;
public static readonly emp = chalk.green; private static mainDeclared = false;
public static readonly wrn = chalk.yellow; public static readonly loglevel = +LOG_LEVEL;
public static readonly err = chalk.red; private static readonly wrn = yellow("[WARN] ");
private static readonly err = red("[ERROR] ");
public static readonly bold = chalk.bold; private static readonly main = bold.gray(
public static readonly itl = chalk.italic; `[ConfessBot | ${green("Anonymous ")}] `
public static readonly udln = chalk.underline;
private static readonly _wrn = Logger.wrn("[WARN] ");
private static readonly _err = Logger.err("[ERROR] ");
public static readonly anon = Logger.bold.gray(
`[ConfessBot | ${Logger.emp("Anonymous ")}] `
); );
constructor(origin?: string) { constructor(origin?: string) {
if (origin === "Main" && Logger.mainDeclared) {
throw new Error("Main Logger has already been instantiated.");
}
this.name = origin ?? "Anonymous"; this.name = origin ?? "Anonymous";
this.isMain = this.name === "Main"; this.isMain = this.name === "Main";
Logger.mainDeclared = this.name === "Main";
this._main = Logger.bold.gray(`[ConfessBot | ${Logger.emp(this.name)}] `); this.main = bold.gray(`[ConfessBot | ${green(this.name)}] `);
} }
private static argDet(args: any[]): any | any[] { public log(...args: any[]): void {
return args.length === 1 ? args : args[0]; (this.isMain || Logger.loglevel > 2) && console.log(this.main, ...args);
} }
public log(text?: any, ...args: any[]): void { public static log(...args: any[]): void {
if (this.isMain || this._loglevel > 2) { this.loglevel > 2 && console.log(this.main, ...args);
args.length
? console.log(this._main + text, Logger.argDet(args))
: console.log(this._main + text);
}
} }
public static log(text?: any, ...args: any[]): void { public warn(...args: any[]): void {
args.length Logger.loglevel > 1 && console.warn(Logger.wrn + this.main, ...args);
? console.log(Logger.anon + text, Logger.argDet(args))
: console.log(Logger.anon + text);
} }
public warn(text?: any, ...args: any[]): void { public static warn(...args: any[]): void {
if (this._loglevel > 1) { this.loglevel > 1 && console.warn(this.wrn, ...args);
args.length
? console.warn(Logger._wrn + this._main + text, Logger.argDet(args))
: console.warn(Logger._wrn + this._main + text);
}
} }
public static warn(text?: any, ...args: any[]): void { public error(...args: any[]): void {
args.length console.error(Logger.err + this.main, ...args);
? console.warn(Logger._wrn + Logger.anon + text, Logger.argDet(args))
: console.warn(Logger._wrn + Logger.anon + text);
} }
public error(text?: any, ...args: any[]): void { public static error(...args: any[]): void {
args.length console.error(this.err + this.main, ...args);
? console.error(Logger._err + this._main + text, Logger.argDet(args))
: console.error(Logger._err + this._main + text);
}
public static error(text?: any, ...args: any[]): void {
args.length
? console.error(Logger._err + Logger.anon + text, Logger.argDet(args))
: console.error(Logger._err + Logger.anon + text);
} }
} }