From d7dcece7fe67298d843ab60bbe76024b297f753a Mon Sep 17 00:00:00 2001 From: Spencer Ofwiti Date: Sun, 14 Mar 2021 11:11:37 +0300 Subject: [PATCH] Add logging service. - Implements custom logging functionality. --- src/app/_models/account.spec.ts | 7 ---- src/app/_services/logging.service.spec.ts | 16 ++++++++ src/app/_services/logging.service.ts | 48 +++++++++++++++++++++++ 3 files changed, 64 insertions(+), 7 deletions(-) delete mode 100644 src/app/_models/account.spec.ts create mode 100644 src/app/_services/logging.service.spec.ts create mode 100644 src/app/_services/logging.service.ts diff --git a/src/app/_models/account.spec.ts b/src/app/_models/account.spec.ts deleted file mode 100644 index faee6b2..0000000 --- a/src/app/_models/account.spec.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { Account } from './account'; - -describe('Account', () => { - it('should create an instance', () => { - expect(new Account()).toBeTruthy(); - }); -}); diff --git a/src/app/_services/logging.service.spec.ts b/src/app/_services/logging.service.spec.ts new file mode 100644 index 0000000..868d58b --- /dev/null +++ b/src/app/_services/logging.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { LoggingService } from './logging.service'; + +describe('LoggingService', () => { + let service: LoggingService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(LoggingService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/_services/logging.service.ts b/src/app/_services/logging.service.ts new file mode 100644 index 0000000..719d151 --- /dev/null +++ b/src/app/_services/logging.service.ts @@ -0,0 +1,48 @@ +import {Injectable} from '@angular/core'; +import {NGXLogger} from 'ngx-logger'; +import {environment} from '@src/environments/environment'; + +@Injectable({ + providedIn: 'root' +}) +export class LoggingService { + env: string; + canDebug: boolean; + + constructor(private logger: NGXLogger) { + // TRACE|DEBUG|INFO|LOG|WARN|ERROR|FATAL|OFF + this.env = environment.production ? 'Production' : 'Development'; + + if (this.env === 'Development') { + this.sendInfoLevelMessage('Dropping into debug mode'); + } + } + + sendTraceLevelMessage(message, source, error): void { + this.logger.trace(message, source, error); + } + + sendDebugLevelMessage(message, source, error): void { + this.logger.debug(message, source, error); + } + + sendInfoLevelMessage(message): void { + this.logger.info(message); + } + + sendLogLevelMessage(message, source, error): void { + this.logger.log(message, source, error); + } + + sendWarnLevelMessage(message, error): void { + this.logger.warn(message, error); + } + + sendErrorLevelMessage(message, source, error): void { + this.logger.error(message, source, error); + } + + sendFatalLevelMessage(message, source, error): void { + this.logger.fatal(message, source, error); + } +}