fix: refactor code

This commit is contained in:
abdou6666 2025-01-27 17:11:47 +01:00
parent 32d57874cd
commit 495cc06094

View File

@ -6,83 +6,82 @@
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file). * 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/ */
import { ConsoleLogger, Inject, Injectable, Scope } from '@nestjs/common'; import {
ConsoleLogger,
Inject,
Injectable,
LogLevel,
Scope,
} from '@nestjs/common';
import { INQUIRER } from '@nestjs/core'; import { INQUIRER } from '@nestjs/core';
type TLog = 'log' | 'warn' | 'error' | 'verbose' | 'debug';
@Injectable({ scope: Scope.TRANSIENT }) @Injectable({ scope: Scope.TRANSIENT })
export class LoggerService extends ConsoleLogger { export class LoggerService extends ConsoleLogger {
private logLevels: LogLevel[] = [];
constructor(@Inject(INQUIRER) private parentClass: object) { constructor(@Inject(INQUIRER) private parentClass: object) {
super(parentClass.constructor.name); super(parentClass.constructor.name);
this.initLogLevels();
} }
log(message: any, ...args: any[]) { log(message: string, ...args: any[]) {
if (!this.isLevelEnabled('log')) {
return;
}
super.log(message); super.log(message);
this.logArguments('log', args); this.logArguments('log', args);
} }
error(message: any, ...args: any[]) { error(message: string, ...args: any[]) {
if (!this.isLevelEnabled('error')) {
return;
}
super.error(message); super.error(message);
this.logArguments('error', args); this.logArguments('error', args);
} }
warn(message: any, ...args: any[]) { warn(message: string, ...args: any[]) {
if (!this.isLevelEnabled('warn')) {
return;
}
super.warn(message); super.warn(message);
this.logArguments('warn', args); this.logArguments('warn', args);
} }
debug(message: any, ...args: any[]) { debug(message: string, ...args: any[]) {
if (!this.isLevelEnabled('debug')) {
return;
}
super.debug(message); super.debug(message);
this.logArguments('debug', args); this.logArguments('debug', args);
} }
verbose(message: any, ...args: any[]) { verbose(message: string, ...args: any[]) {
if (!this.isLevelEnabled('verbose')) {
return;
}
super.verbose(message); super.verbose(message);
this.logArguments('verbose', args); this.logArguments('verbose', args);
} }
private safeStringifyReplacer() { fatal(message: string, ...args: any[]) {
const seen = new WeakSet(); if (!this.isLevelEnabled('fatal')) {
return (key: string, value: any) => { return;
if (typeof value === 'object' && value !== null) {
if (seen.has(value)) return '[Circular Reference]';
seen.add(value);
} }
return value; super.fatal(message);
}; this.logArguments('fatal', args);
} }
private handleError(type: TLog, error: Error) { private logArguments(type: LogLevel, args: any[]) {
const errorInfo = { args.forEach((arg) => {
message: error.message, super[type](arg);
name: error.name, });
stack: error.stack,
};
super[type](JSON.stringify(errorInfo, this.safeStringifyReplacer()));
} }
private logArguments(type: TLog, args: any[]) { private initLogLevels() {
const isDevMode = process.env.NODE_ENV?.includes('dev'); process.env.NODE_ENV?.includes('dev')
if (isDevMode) { ? this.logLevels.push('log', 'debug', 'error', 'verbose', 'fatal', 'warn')
args.forEach((arg) => { : this.logLevels.push('log', 'warn', 'error');
if (arg instanceof Error) { super.setLogLevels(this.logLevels);
this.handleError(type, arg);
} else if (typeof arg === 'object' && arg !== null) {
super[type](JSON.stringify(arg, this.safeStringifyReplacer()));
} else {
super[type](arg);
}
});
} else {
// In production we do not stringify the arguments for performance reasons, except for errors
args.forEach((arg) => {
if (arg instanceof Error) {
this.handleError(type, arg);
} else {
super[type](arg);
}
});
}
} }
} }