Last active
February 7, 2020 19:11
-
-
Save jarhoads/9b3daf3b982b8d4d34998a0d0272ddd1 to your computer and use it in GitHub Desktop.
A simple angular logging service
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 } from '@angular/core'; | |
import { HttpClient } from '@angular/common/http'; | |
import {LogLevel, LogEntry, ILoggable, ConsoleLog, LocalStorageLog, WebApiLog} from './models/logModels'; | |
import { Observable, of } from 'rxjs'; | |
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class LogService { | |
private level: LogLevel = LogLevel.All; | |
private logWithDate = true; | |
private testEnv = false; | |
private logs: ILoggable[] = []; | |
private consoleLogger: ConsoleLog; | |
private localStorageLogger: LocalStorageLog; | |
private webApiLogger: WebApiLog; | |
constructor(private http: HttpClient) { | |
this.testEnv = this.isTestEnv(); | |
this.logBuilder(); | |
} | |
private logBuilder(): void { | |
this.consoleLogger = new ConsoleLog(); | |
this.localStorageLogger = new LocalStorageLog(); | |
this.logs.push(this.consoleLogger); | |
this.logs.push(this.localStorageLogger); | |
this.webApiLogger = new WebApiLog(this.http); | |
} | |
private isTestEnv(): boolean { | |
const testEnvironments = ['test', 'dev', 'it', 'localhost']; | |
console.log(`!!&&--> location host: ${location.host}`); | |
return (testEnvironments.filter(env => location.host.indexOf(env) > -1).length > 0); | |
} | |
debug(msg: string, ...optionalParams: any[]) { | |
this.writeToLog(msg, LogLevel.Debug, optionalParams); | |
} | |
info(msg: string, ...optionalParams: any[]) { | |
this.writeToLog(msg, LogLevel.Info, optionalParams); | |
} | |
warn(msg: string, ...optionalParams: any[]) { | |
this.writeToLog(msg, LogLevel.Warn, optionalParams); | |
} | |
error(msg: string, ...optionalParams: any[]) { | |
msg = this.includeLogMessages(msg); | |
this.localStorageLogger.clear(); | |
const message = 'Local Storage Details Cleared - Cleared on error()'; | |
console.log(message); | |
return this.writeToWebLog(msg); | |
} | |
fatal(msg: string, ...optionalParams: any[]) { | |
this.writeToLog(msg, LogLevel.Fatal, optionalParams); | |
} | |
log(msg: string, ...optionalParams: any[]) { | |
this.writeToLog(msg, LogLevel.All, optionalParams); | |
} | |
web(msg: string) { | |
this.writeToWebLog(msg).subscribe(result => console.log(result)); | |
} | |
initLocalEntries(appMessage: string = ''): void { | |
const message = 'Local Storage Details Cleared - ' + appMessage; | |
console.log(message); | |
if (!this.localStorageLogger.empty()) { | |
const msg = this.includeLogMessages(message); | |
console.log(`write to webAPi: ${msg}`); | |
this.web(msg); | |
} | |
this.localStorageLogger.clear(); | |
} | |
private includeLogMessages(message: string): string { | |
this.log(message); | |
message += (', ' + this.localStorageLogger.getLogDetails()); | |
return message; | |
} | |
private writeToWebLog(msg: string): Observable<any> { | |
// "build" log entry | |
const entry: LogEntry = new LogEntry(); | |
entry.message = msg; | |
entry.level = LogLevel.Web; | |
entry.extraInfo = []; | |
entry.logWithDate = this.logWithDate; | |
if (this.testEnv) { | |
console.log(`webApi logging suppressed in test`); | |
// tslint:disable-next-line:quotemark | |
console.log(`Web API Entry: ${JSON.stringify(entry).replace(/\\"/g, "'")}`); | |
return of(true); | |
} else { | |
return this.webApiLogger.log(entry); | |
} | |
} | |
private writeToLog(msg: string, level: LogLevel, params: any[]) { | |
if (this.shouldLog(level)) { | |
const entry: LogEntry = new LogEntry(); | |
entry.message = msg; | |
entry.level = level; | |
entry.extraInfo = params; | |
entry.logWithDate = this.logWithDate; | |
this.logs.forEach(logger => { | |
logger.log(entry).subscribe(response => { | |
if (logger.logLocation === 'logDetails') { | |
console.log('written to Local Storage[' + logger.logLocation + ']'); | |
} | |
}); | |
}); | |
} | |
} | |
private shouldLog(level: LogLevel): boolean { | |
let ret = false; | |
if ((level >= this.level && | |
level !== LogLevel.Off) || | |
this.level === LogLevel.All) { | |
ret = true; | |
} | |
return ret; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment