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).
*/
import { ConsoleLogger, Inject, Injectable, Scope } from '@nestjs/common';
import {
ConsoleLogger,
Inject,
Injectable,
LogLevel,
Scope,
} from '@nestjs/common';
import { INQUIRER } from '@nestjs/core';
type TLog = 'log' | 'warn' | 'error' | 'verbose' | 'debug';
@Injectable({ scope: Scope.TRANSIENT })
export class LoggerService extends ConsoleLogger {
private logLevels: LogLevel[] = [];
constructor(@Inject(INQUIRER) private parentClass: object) {
super(parentClass.constructor.name);
this.initLogLevels();
}
log(message: any, ...args: any[]) {
log(message: string, ...args: any[]) {
if (!this.isLevelEnabled('log')) {
return;
}
super.log(message);
this.logArguments('log', args);
}
error(message: any, ...args: any[]) {
error(message: string, ...args: any[]) {
if (!this.isLevelEnabled('error')) {
return;
}
super.error(message);
this.logArguments('error', args);
}
warn(message: any, ...args: any[]) {
warn(message: string, ...args: any[]) {
if (!this.isLevelEnabled('warn')) {
return;
}
super.warn(message);
this.logArguments('warn', args);
}
debug(message: any, ...args: any[]) {
debug(message: string, ...args: any[]) {
if (!this.isLevelEnabled('debug')) {
return;
}
super.debug(message);
this.logArguments('debug', args);
}
verbose(message: any, ...args: any[]) {
verbose(message: string, ...args: any[]) {
if (!this.isLevelEnabled('verbose')) {
return;
}
super.verbose(message);
this.logArguments('verbose', args);
}
private safeStringifyReplacer() {
const seen = new WeakSet();
return (key: string, value: any) => {
if (typeof value === 'object' && value !== null) {
if (seen.has(value)) return '[Circular Reference]';
seen.add(value);
}
return value;
};
}
private handleError(type: TLog, error: Error) {
const errorInfo = {
message: error.message,
name: error.name,
stack: error.stack,
};
super[type](JSON.stringify(errorInfo, this.safeStringifyReplacer()));
}
private logArguments(type: TLog, args: any[]) {
const isDevMode = process.env.NODE_ENV?.includes('dev');
if (isDevMode) {
args.forEach((arg) => {
if (arg instanceof Error) {
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);
}
});
fatal(message: string, ...args: any[]) {
if (!this.isLevelEnabled('fatal')) {
return;
}
super.fatal(message);
this.logArguments('fatal', args);
}
private logArguments(type: LogLevel, args: any[]) {
args.forEach((arg) => {
super[type](arg);
});
}
private initLogLevels() {
process.env.NODE_ENV?.includes('dev')
? this.logLevels.push('log', 'debug', 'error', 'verbose', 'fatal', 'warn')
: this.logLevels.push('log', 'warn', 'error');
super.setLogLevels(this.logLevels);
}
}