From a94d683293574d60b1a61c71b3f3b21b000853aa Mon Sep 17 00:00:00 2001 From: powermaker450 Date: Wed, 21 Aug 2024 11:49:54 -0400 Subject: [PATCH] Add configurable log levels --- src/environment.d.ts | 1 + src/utils/logger.ts | 31 +++++++++++++++++++++---------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/environment.d.ts b/src/environment.d.ts index 2959f06..5d498ff 100644 --- a/src/environment.d.ts +++ b/src/environment.d.ts @@ -2,6 +2,7 @@ declare global { namespace NodeJS { interface ProcessEnv { PORT: string; + LOG_LEVEL: string; } } } diff --git a/src/utils/logger.ts b/src/utils/logger.ts index 57b08db..c9f33d3 100644 --- a/src/utils/logger.ts +++ b/src/utils/logger.ts @@ -4,30 +4,41 @@ export class Logger { private _wrn: string; private _err: string; private _main: string; + private _logLevel = +process.env.LOG_LEVEL || 1; + public name: string; + public isMainFunction: boolean; constructor(origin?: string) { this._wrn = chalk.yellow("[WARN] "); this._err = chalk.red("[ERROR] "); + this.name = origin ?? "Anonymous"; + this.isMainFunction = this.name === "Main"; - this._main = chalk.bold.gray(`[SRS | ${emp(origin ?? "Anonymous")}] `); + this._main = chalk.bold.gray(`[SRS | ${emp(this.name)}] `); } public log(text?: any, args?: any): void { - args - ? console.log(this._main + text, args) - : console.log(this._main + text); + if (this.isMainFunction || this._logLevel === 0) { + args + ? console.log(this._main + text, args) + : console.log(this._main + text); + } } public warn(text?: any, args?: any): void { - args - ? console.warn(this._main + this._wrn + text, args) - : console.warn(this._main + this._wrn + text); + if (this._logLevel === 1) { + args + ? console.warn(this._main + this._wrn + text, args) + : console.warn(this._main + this._wrn + text); + } } public error(text?: any, args?: any): void { - args - ? console.error(this._main + this._err + text, args) - : console.error(this._main + this._err + text); + if (this._logLevel === 2) { + args + ? console.error(this._main + this._err + text, args) + : console.error(this._main + this._err + text); + } } }