All files / src/lib/services/logger logger.service.ts

55.17% Statements 16/29
41.66% Branches 10/24
33.33% Functions 3/9
47.82% Lines 11/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 491x 1x 1x 1x 1x       1x   1x 1x 1x                                                               1x     1x  
import { Injectable } from '@angular/core';
import { ILogService } from '../../models/logger.model';
I
import { LibraryConfigService } from '../library-config/library-config.service';

@Injectable({
	providedIn: 'root'
})
export class LoggerService implements ILogService {
	public log;
	public LOG_FNS = [];
	public MSG_PREFIXES = [
		['[', ']'],
		['[', '] WARN: '],
		['[', '] ERROR: ']
	];
 
	constructor(private libraryConfigSrv: LibraryConfigService) {
	}

	private getLoggerFns(prefix: string) {
		this.log = window.console;
		this.LOG_FNS = [this.log.log, this.log.warn, this.log.error];
		const loggerFns = this.LOG_FNS.map((logTemplFn, i) => {
			return logTemplFn.bind(this.log, this.MSG_PREFIXES[i][0] + prefix + this.MSG_PREFIXES[i][1]);
		});
		return loggerFns;
	}

	public get(prefix: string) {
		const prodMode = this.libraryConfigSrv.isProduction();
		const loggerService = this;
		return {
			d: function(...args: any[]) {
				if (!prodMode) {
					loggerService.getLoggerFns(prefix)[0].apply(this.log, arguments);
				}
			},
			w: function(...args: any[]) {
				loggerService.getLoggerFns(prefix)[1].apply(this.log, arguments);
 
			},
			e: function(...args: any[]) {
				loggerService.getLoggerFns(prefix)[2].apply(this.log, arguments);
			}
		};
	}
}