From 57bad287468f5da50a1f06af71f7acf47a725685 Mon Sep 17 00:00:00 2001 From: Hitesh Date: Mon, 26 Aug 2024 11:51:55 +0530 Subject: [PATCH] add logger service --- src/app/core/services/logger.service.ts | 159 ++++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 src/app/core/services/logger.service.ts diff --git a/src/app/core/services/logger.service.ts b/src/app/core/services/logger.service.ts new file mode 100644 index 0000000..404e643 --- /dev/null +++ b/src/app/core/services/logger.service.ts @@ -0,0 +1,159 @@ +import { Injectable } from '@angular/core'; + +@Injectable({ + providedIn: 'root', +}) +export class LoggerService { + private sessionId = 'sample-session-id'; // Replace with actual session ID logic + + // constructor() {} + + private formatLog( + level: string, + severity: string, + tracebackId: string, + screenName: string, + source: string, + deviceInfo: string, + data: string, + message: string, + stackTrace = '', + timestamp = '', + ): string { + return JSON.stringify({ + timestamp, + level, + tracebackId, + severity, + sessionId: this.sessionId, + screenName, + source, + deviceInfo, + data, + message, + stackTrace, + }); + } + + log( + level: string, + severity: string, + tracebackId: string, + screenName: string, + source: string, + deviceInfo: string, + data: string, + message: string, + stackTrace?: string, + timestamp?: string, + ): void { + const currentTimestamp = timestamp || new Date().toISOString(); + const formattedLog = this.formatLog( + level, + severity, + tracebackId, + screenName, + source, + deviceInfo, + data, + message, + stackTrace || '', + timestamp || currentTimestamp, + ); + // eslint-disable-next-line no-console + console.log(formattedLog); + } + + // Additional methods for different log levels + info( + severity: string, + tracebackId: string, + screenName: string, + source: string, + deviceInfo: string, + data: string, + message: string, + stackTrace?: string, + ): void { + this.log( + 'INFO', + severity || 'LOW', + tracebackId, + screenName, + source, + deviceInfo, + data, + message, + stackTrace, + ); + } + + warn( + severity: string, + tracebackId: string, + screenName: string, + source: string, + deviceInfo: string, + data: string, + message: string, + stackTrace?: string, + ): void { + this.log( + 'WARN', + severity || 'LOW', + tracebackId, + screenName, + source, + deviceInfo, + data, + message, + stackTrace, + ); + } + + error( + severity: string, + tracebackId: string, + screenName: string, + source: string, + deviceInfo: string, + data: string, + message: string, + stackTrace?: string, + ): void { + this.log( + 'ERROR', + severity || 'MEDIUM', + tracebackId, + screenName, + source, + deviceInfo, + data, + message, + stackTrace, + ); + } + + fatal( + severity: string, + tracebackId: string, + screenName: string, + source: string, + deviceInfo: string, + data: string, + message: string, + stackTrace?: string, + ): void { + this.log( + 'FATAL', + severity || 'HIGH', + tracebackId, + screenName, + source, + deviceInfo, + data, + message, + stackTrace, + ); + } +}