ov-components: Refactor LoggerService to improve logger function creation and cache handling

master
Carlos Santos 2025-08-19 10:50:13 +02:00
parent 57e76fe69b
commit 4fb4878342
1 changed files with 53 additions and 25 deletions

View File

@ -1,6 +1,5 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { ILogService } from '../../models/logger.model'; import { ILogService, ILogger } from '../../models/logger.model';
import { GlobalConfigService } from '../config/global-config.service'; import { GlobalConfigService } from '../config/global-config.service';
/** /**
@ -10,42 +9,71 @@ import { GlobalConfigService } from '../config/global-config.service';
providedIn: 'root' providedIn: 'root'
}) })
export class LoggerService implements ILogService { export class LoggerService implements ILogService {
public log; private log: Console;
public LOG_FNS = []; private LOG_FNS: Function[] = [];
public MSG_PREFIXES = [ private MSG_PREFIXES: string[][] = [
['[', ']'], ['[', '] DEBUG: '],
['[', '] WARN: '], ['[', '] WARN: '],
['[', '] ERROR: '] ['[', '] ERROR: ']
]; ];
private loggerCache: Map<string, ILogger> = new Map();
constructor(private globalService: GlobalConfigService) { constructor(private globalService: GlobalConfigService) {
this.initializeLogger();
} }
private getLoggerFns(prefix: string) { private initializeLogger(): void {
this.log = window.console; this.log = window.console;
this.LOG_FNS = [this.log.log, this.log.warn, this.log.error]; this.LOG_FNS = [this.log.log.bind(this.log), this.log.warn.bind(this.log), this.log.error.bind(this.log)];
const loggerFns = this.LOG_FNS.map((logTemplFn, i) => {
return logTemplFn.bind(this.log, this.MSG_PREFIXES[i][0] + prefix + this.MSG_PREFIXES[i][1]);
});
return loggerFns;
} }
public get(prefix: string) { private createLoggerFunctions(prefix: string): [(...args: any[]) => void, (...args: any[]) => void, (...args: any[]) => void] {
const prodMode = this.globalService.isProduction(); const prodMode = this.globalService.isProduction();
const loggerService = this;
return {
d: function(...args: any[]) {
if (!prodMode) {
loggerService.getLoggerFns(prefix)[0].apply(this.log, arguments);
}
},
w: function(...args: any[]) {
loggerService.getLoggerFns(prefix)[1].apply(this.log, arguments);
}, const debugFn = (...args: any[]): void => {
e: function(...args: any[]) { if (!prodMode) {
loggerService.getLoggerFns(prefix)[2].apply(this.log, arguments); // Only log debug messages in non-production mode
this.LOG_FNS[0](this.MSG_PREFIXES[0][0] + prefix + this.MSG_PREFIXES[0][1], ...args);
} }
}; };
const warnFn = (...args: any[]): void => {
this.LOG_FNS[1](this.MSG_PREFIXES[1][0] + prefix + this.MSG_PREFIXES[1][1], ...args);
};
const errorFn = (...args: any[]): void => {
this.LOG_FNS[2](this.MSG_PREFIXES[2][0] + prefix + this.MSG_PREFIXES[2][1], ...args);
};
return [debugFn, warnFn, errorFn];
}
public get(prefix: string): ILogger {
// Check cache first
if (this.loggerCache.has(prefix)) {
return this.loggerCache.get(prefix)!;
}
// Create new logger functions
const [debugFn, warnFn, errorFn] = this.createLoggerFunctions(prefix);
const logger: ILogger = {
d: debugFn,
w: warnFn,
e: errorFn
};
// Cache the logger
this.loggerCache.set(prefix, logger);
return logger;
}
/**
* Clears the logger cache. Useful for testing or when configuration changes.
* @internal
*/
public clearCache(): void {
this.loggerCache.clear();
} }
} }