diff --git a/src/@loafer/ng-logger/service/logger.service.ts b/src/@loafer/ng-logger/service/logger.service.ts index 622c564..768a5de 100644 --- a/src/@loafer/ng-logger/service/logger.service.ts +++ b/src/@loafer/ng-logger/service/logger.service.ts @@ -8,6 +8,8 @@ import { _LOGGER_CONFIG, } from '../core'; +export type ConsoleFunc = (message?: any, ...optionalParams: any[]) => void; + @Injectable() export class LoggerService { private _isIE: boolean; @@ -18,30 +20,59 @@ export class LoggerService { ) { this._isIE = isPlatformBrowser(platformId) && !!(navigator.userAgent.indexOf('MSIE') !== -1 || navigator.userAgent.match(/Trident\//) || navigator.userAgent.match(/Edge\//)); + } - public trace(message, ...additional: any[]): void { - this._log(LoggerLevel.TRACE, message, additional); + public get trace(): ConsoleFunc { + return this.getConsoleMethod(LoggerLevel.TRACE); + } + public get debug(): ConsoleFunc { + return this.getConsoleMethod(LoggerLevel.DEBUG); + } + public get info(): ConsoleFunc { + return this.getConsoleMethod(LoggerLevel.INFO); + } + public get log(): ConsoleFunc { + return this.getConsoleMethod(LoggerLevel.LOG); + } + public get warn(): ConsoleFunc { + return this.getConsoleMethod(LoggerLevel.WARN); + } + public get error(): ConsoleFunc { + return this.getConsoleMethod(LoggerLevel.ERROR); } - public debug(message, ...additional: any[]): void { - this._log(LoggerLevel.DEBUG, message, additional); + private _console_log: ConsoleFunc; + + private getConsoleMethod(level: LoggerLevel): ConsoleFunc { + if (level < this.config.level) { + return (message, ...additional: any[]) => {}; + } + + if (this._isIE) { + switch (level) { + case LoggerLevel.WARN: + return console.warn.bind(console, ...this.getLogHeader(level)); + case LoggerLevel.ERROR: + return console.error.bind(console, ...this.getLogHeader(level)); + case LoggerLevel.INFO: + return console.info.bind(console, ...this.getLogHeader(level)); + default: + return console.log.bind(console, ...this.getLogHeader(level)); + } + } else { + return console.log.bind(console, ...this.getLogHeader(level)); + } } - public info(message, ...additional: any[]): void { - this._log(LoggerLevel.INFO, message, additional); - } - - public log(message, ...additional: any[]): void { - this._log(LoggerLevel.LOG, message, additional); - } - - public warn(message, ...additional: any[]): void { - this._log(LoggerLevel.WARN, message, additional); - } - - public error(message, ...additional: any[]): void { - this._log(LoggerLevel.ERROR, message, additional); + private getLogHeader(level: LoggerLevel): any[] { + const params: any[] = []; + params.push(`%c${this._timestamp()} [${LoggerLevelName[level]}]`); + if (!this._isIE) { + const color = this._getColor(level); + params.push(`color:${color}`); + } + return params; } private _timestamp(): string { @@ -77,22 +108,33 @@ export class LoggerService { const color = this._getColor(level); - console.log(`%c${this._timestamp()} [${LoggerLevelName[level]}]`, `color:${color}`, message, ...additional); + const params: any[] = []; + params.push(`%c${this._timestamp()} [${LoggerLevelName[level]}]`); + params.push(`color:${color}`); + params.push(message); + params.push(...additional); + + console.log.apply(console, params); } private _logIE(level: LoggerLevel, message: string, additional: any[] = []): void { + const params: any[] = []; + params.push(`${this._timestamp()} [${LoggerLevelName[level]}] `); + params.push(message); + params.push(...additional); + switch (level) { case LoggerLevel.WARN: - console.warn(`${this._timestamp()} [${LoggerLevelName[level]}] `, message, ...additional); + console.warn.apply(console, params); break; case LoggerLevel.ERROR: - console.error(`${this._timestamp()} [${LoggerLevelName[level]}] `, message, ...additional); + console.error.apply(console, params); break; case LoggerLevel.INFO: - console.log(`${this._timestamp()} [${LoggerLevelName[level]}] `, message, ...additional); + console.info.apply(console, params); break; default: - console.log(`${this._timestamp()} [${LoggerLevelName[level]}] `, message, ...additional); + console.log.apply(console, params); } } diff --git a/src/packages/discovery/subscriber/discovery.subscriber.ts b/src/packages/discovery/subscriber/discovery.subscriber.ts index e1b6898..a603981 100644 --- a/src/packages/discovery/subscriber/discovery.subscriber.ts +++ b/src/packages/discovery/subscriber/discovery.subscriber.ts @@ -20,6 +20,7 @@ export class DiscoverySubscriber { private store: Store, private loggerService: LoggerService, ) { + this.loggerService.debug('dddddd'); } @RPCSubscriber({method: 'DiscoveryService.discoveredZone'})