Created
June 21, 2019 02:59
-
-
Save buildmotion/08d04325e3bb2797e33699c143749849 to your computer and use it in GitHub Desktop.
This file contains 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
import { Injectable, Optional } from "@angular/core"; | |
import { Severity } from "./severity.enum"; | |
import { IConfiguration, LoggingConfig } from "@angularlicious/configuration"; | |
import { ConfigurationService } from "@angularlicious/configuration"; | |
import { LogEntry } from "./log-entry"; | |
import { Subject, ReplaySubject } from "rxjs"; | |
import { ILogEntry } from "./i-log-entry"; | |
import { ILoggingConfig } from "./config/i-logging-config"; | |
@Injectable() | |
export class LoggingService { | |
serviceName = "LoggingService"; | |
source: string; | |
severity: Severity; | |
message: string; | |
timestamp: Date; | |
applicationName = "application"; | |
version = "0.0.0"; | |
isProduction: boolean; | |
config: LoggingConfig; | |
logEntries$: Subject<ILogEntry> = new ReplaySubject<ILogEntry>(1); | |
constructor(@Optional() public configService: ConfigurationService) { | |
this.timestamp = new Date(Date.now()); | |
this.log( | |
this.serviceName, | |
Severity.Information, | |
`Starting logging service at: ${this.timestamp}` | |
); | |
if (configService) { | |
this.configService.settings$.subscribe(settings => | |
this.handleSettings(settings) | |
); | |
} | |
} | |
handleSettings(settings: IConfiguration) { | |
this.config = settings as LoggingConfig; | |
this.applicationName = | |
this.config && this.config.loggingConfig.applicationName | |
? this.config.loggingConfig.applicationName | |
: "application"; | |
this.version = | |
this.config && this.config.loggingConfig.version | |
? this.config.loggingConfig.version | |
: "0.0.0"; | |
this.isProduction = | |
this.config && this.config.loggingConfig.isProduction | |
? this.config.loggingConfig.isProduction | |
: false; | |
} | |
log(source: string, severity: Severity, message: string, tags?: string[]) { | |
this.source = `${this.applicationName}.${source}`; | |
this.severity = severity; | |
this.message = message; | |
this.timestamp = new Date(Date.now()); | |
const logEntry = new LogEntry( | |
this.applicationName, | |
this.source, | |
this.severity, | |
this.message, | |
tags | |
); | |
this.logEntries$.next(logEntry); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment