Add configurable log levels
This commit is contained in:
parent
0aea8e7464
commit
a94d683293
1
src/environment.d.ts
vendored
1
src/environment.d.ts
vendored
|
@ -2,6 +2,7 @@ declare global {
|
|||
namespace NodeJS {
|
||||
interface ProcessEnv {
|
||||
PORT: string;
|
||||
LOG_LEVEL: string;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue