Skip to content

Commit

Permalink
add logger service
Browse files Browse the repository at this point in the history
  • Loading branch information
Hitesh-Kandpal committed Aug 26, 2024
1 parent 10ffe80 commit 57bad28
Showing 1 changed file with 159 additions and 0 deletions.
159 changes: 159 additions & 0 deletions src/app/core/services/logger.service.ts
Original file line number Diff line number Diff line change
@@ -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,
);
}
}

0 comments on commit 57bad28

Please sign in to comment.