From 5aa580d693370caa278488ec6b51d37943ee63cf Mon Sep 17 00:00:00 2001 From: Holger Stolzenberg Date: Sat, 16 Mar 2024 09:14:49 +0100 Subject: [PATCH] Introduce logging module --- src/app/app.module.ts | 7 ++----- src/app/logging/logging.module.ts | 19 +++++++++++++++++++ src/app/logging/logging.service.ts | 8 ++++++++ src/app/notifications/notification.service.ts | 11 ++++++----- 4 files changed, 35 insertions(+), 10 deletions(-) create mode 100644 src/app/logging/logging.module.ts create mode 100644 src/app/logging/logging.service.ts diff --git a/src/app/app.module.ts b/src/app/app.module.ts index f84d195..fb1ddf8 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -3,12 +3,10 @@ import { AppComponent } from './app.component'; import { HeaderComponent } from './header/header.component'; import { BrowserModule } from '@angular/platform-browser'; import { ToolbarComponent } from './toolbar/toolbar.component'; -import { LoggerModule, NgxLoggerLevel } from 'ngx-logger'; import { provideAnimationsAsync } from '@angular/platform-browser/animations/async'; import { MapComponent } from './map/map.component'; import { MatIcon } from '@angular/material/icon'; import { MatButton } from '@angular/material/button'; -import { environment } from '../environments/environment'; import { OptionPaneComponent } from './option-pane/option-pane.component'; import { MatSlideToggle } from '@angular/material/slide-toggle'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; @@ -18,6 +16,7 @@ import { NotificationsModule } from './notifications/notifications.module'; import { MapModule } from './map/map.module'; import { MatSlider, MatSliderModule, MatSliderThumb } from '@angular/material/slider'; import { FormsModule } from '@angular/forms'; +import { LoggingModule } from './logging/logging.module'; @NgModule({ declarations: [HeaderComponent, ToolbarComponent, MapComponent, AppComponent, OptionPaneComponent], @@ -31,9 +30,7 @@ import { FormsModule } from '@angular/forms'; MatIcon, MatButton, MatSlideToggle, - LoggerModule.forRoot({ - level: environment.prodMode ? NgxLoggerLevel.WARN : NgxLoggerLevel.DEBUG - }), + LoggingModule, I18nModule, NotificationsModule, MapModule, diff --git a/src/app/logging/logging.module.ts b/src/app/logging/logging.module.ts new file mode 100644 index 0000000..d5b15bd --- /dev/null +++ b/src/app/logging/logging.module.ts @@ -0,0 +1,19 @@ +import { NgModule } from '@angular/core'; +import { LoggingService } from './logging.service'; +import { LoggerModule, NgxLoggerLevel } from 'ngx-logger'; +import { environment } from '../../environments/environment'; + +@NgModule({ + exports: [], + imports: [ + LoggerModule.forRoot({ + level: environment.prodMode ? NgxLoggerLevel.WARN : NgxLoggerLevel.DEBUG + }) + ], + providers: [ + LoggingService + ] +}) +export class LoggingModule { + // nothing here so far +} diff --git a/src/app/logging/logging.service.ts b/src/app/logging/logging.service.ts new file mode 100644 index 0000000..7b9596a --- /dev/null +++ b/src/app/logging/logging.service.ts @@ -0,0 +1,8 @@ +import { Injectable } from '@angular/core'; +import { NGXLogger } from 'ngx-logger'; + +@Injectable() +export class LoggingService { + constructor(private readonly log: NGXLogger) { + } +} \ No newline at end of file diff --git a/src/app/notifications/notification.service.ts b/src/app/notifications/notification.service.ts index 9802b97..c643d6b 100644 --- a/src/app/notifications/notification.service.ts +++ b/src/app/notifications/notification.service.ts @@ -18,7 +18,8 @@ export class NotificationService { private readonly toastrService: ToastrService, private readonly i18nService: I18nService, private readonly log: NGXLogger - ) {} + ) { + } showError(message: string, error: HttpErrorResponse | GeolocationPositionError) { this.log.error(message, error); @@ -28,10 +29,10 @@ export class NotificationService { } showErrorLocalized(messageKey: string, ...additional: unknown[]) { - this.log.error(messageKey, additional); - - firstValueFrom(this.i18nService.translateMultiple(['notifications.error-title', messageKey])).then(values => - this.toastrService.error(values[1] + ' ' + additional, values[0], options) + firstValueFrom(this.i18nService.translateMultiple(['notifications.error-title', messageKey])).then(values => { + this.log.error(values[1], additional); + this.toastrService.error(values[1] + ' ' + additional, values[0], options); + } ); }