-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add logger service and error handler #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
import { NgModule } from '@angular/core'; | ||
import { NgModule, ErrorHandler } from '@angular/core'; | ||
import { BrowserModule } from '@angular/platform-browser'; | ||
|
||
import { AppRoutingModule } from './app-routing.module'; | ||
import { AppComponent } from './app.component'; | ||
import { MyErrorHandler } from './shared/services/error-handler.service'; | ||
|
||
@NgModule({ | ||
declarations: [AppComponent], | ||
imports: [BrowserModule, AppRoutingModule], | ||
providers: [], | ||
providers: [{ provide: ErrorHandler, useClass: MyErrorHandler }], | ||
bootstrap: [AppComponent], | ||
}) | ||
export class AppModule {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export enum LogLevel { | ||
Info = 'INFO', | ||
Warn = 'WARN', | ||
Error = 'ERROR', | ||
Fatal = 'FATAL', | ||
} | ||
|
||
export enum LogSeverity { | ||
Critical = 'CRITICAL', | ||
High = 'HIGH', | ||
Medium = 'MEDIUM', | ||
Low = 'LOW', | ||
} |
16 changes: 16 additions & 0 deletions
16
apps/portal/src/app/shared/services/error-handler.service.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { MyErrorHandler } from './error-handler.service'; | ||
|
||
describe('ErrorHandlerService', () => { | ||
let service: MyErrorHandler; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({}); | ||
service = TestBed.inject(MyErrorHandler); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
}); |
12 changes: 12 additions & 0 deletions
12
apps/portal/src/app/shared/services/error-handler.service.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { ErrorHandler, Injectable } from '@angular/core'; | ||
import { Logger } from './logger.service'; | ||
|
||
@Injectable() | ||
export class MyErrorHandler implements ErrorHandler { | ||
constructor(private logger: Logger) {} | ||
|
||
// eslint-disable-next-line | ||
handleError(error: any): void { | ||
// code to use logger will be done here | ||
} | ||
} | ||
16 changes: 16 additions & 0 deletions
16
apps/portal/src/app/shared/services/logger.service.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { Logger } from './logger.service'; | ||
|
||
describe('LoggerService', () => { | ||
let service: Logger; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({}); | ||
service = TestBed.inject(Logger); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { HttpClient } from '@angular/common/http'; | ||
import { Observable, throwError, catchError } from 'rxjs'; | ||
import { LogLevel, LogSeverity } from '../enum'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class Logger { | ||
private apiUrl = ''; | ||
|
||
constructor(private http: HttpClient) {} | ||
|
||
log( | ||
severity: LogSeverity, | ||
level: LogLevel, | ||
sessionId: string | null, | ||
screenName: string | null, | ||
source: string, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
data: any | null, | ||
message: string, | ||
stackTrace: string | null, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
): Observable<any> { | ||
try { | ||
const timestamp = new Date().toISOString(); | ||
const deviceInfo = this.getDeviceInfo(); | ||
const logObject = { | ||
timestamp, | ||
level, | ||
severity, | ||
sessionId, | ||
screenName, | ||
source, | ||
deviceInfo, | ||
data, | ||
message, | ||
stackTrace, | ||
}; | ||
|
||
// Dummy code for sending API request | ||
return this.http.post(this.apiUrl, logObject).pipe( | ||
catchError((error) => | ||
// eslint-disable-line | ||
throwError(() => new Error(error)), | ||
), | ||
); | ||
} catch (error) { | ||
return throwError(() => new Error('Error occured while logging')); | ||
} | ||
xixas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// eslint-disable-next-line class-methods-use-this, @typescript-eslint/no-explicit-any | ||
private getDeviceInfo(): any { | ||
const { userAgent } = window.navigator; | ||
let platform = ''; | ||
|
||
if (userAgent.match(/Win/)) { | ||
platform = 'Windows'; | ||
} else if (userAgent.match(/Mac/)) { | ||
platform = 'Mac'; | ||
} else if (userAgent.match(/Linux/)) { | ||
platform = 'Linux'; | ||
} else if (userAgent.match(/Android/)) { | ||
platform = 'Android'; | ||
} else if (userAgent.match(/iPhone|iPad|iPod/)) { | ||
platform = 'iOS'; | ||
} else { | ||
platform = 'Other'; | ||
} | ||
|
||
return { | ||
userAgent, | ||
language: window.navigator.language, | ||
platform, | ||
screenResolution: `${window.screen.width}x${window.screen.height}`, | ||
viewport: `${window.innerWidth}x${window.innerHeight}`, | ||
onlineStatus: navigator.onLine, | ||
}; | ||
} | ||
xixas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.