From 495cc060946024544f9f80155d98317514ffe571 Mon Sep 17 00:00:00 2001 From: abdou6666 Date: Mon, 27 Jan 2025 17:11:47 +0100 Subject: [PATCH] fix: refactor code --- api/src/logger/logger.service.ts | 97 ++++++++++++++++---------------- 1 file changed, 48 insertions(+), 49 deletions(-) diff --git a/api/src/logger/logger.service.ts b/api/src/logger/logger.service.ts index cf6bc358..c7e5847c 100644 --- a/api/src/logger/logger.service.ts +++ b/api/src/logger/logger.service.ts @@ -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); } }