I am working on a class that will be used for console logging, but with every call a new object will be added to the console history array. I created a class ConsoleLogger
export class ConsoleLogger {
private static instance: ConsoleLogger | null = null;
private logs: LogEntry[] = [];
private maxLogs = 50;
public static getInstance(): ConsoleLogger {
if (this.instance === null) {
this.instance = new ConsoleLogger();
}
return this.instance;
}
private addLog(type: LogType, message: string): void {
if (this.logs.length >= this.maxLogs) {
this.logs.shift();
}
this.logs.push({ type, message, timestamp: new Date() });
}
public log = (function () {
return Function.prototype.bind.call(console.log, console);
})();
public error = (function () {
return Function.prototype.bind.call(console.error, console);
})();
}
I can use it in .vue and .ts files, with e.g.
ConsoleLogger.getInstance().log('console msg');
and the line information is refering correctly, to my .ts file/.vue file. I am not sure how can I connect addLog line to each of these methods, as I cannot refer to this.addLog in this exact log function, and if I try to change the log method that I will make this.addLog available, then I am losing the proper line information as it starts refering to the line in my consoleLogger.ts.
How can I not lose the line information and also use my addLog method properly?