This commit is contained in:
crusader 2018-03-25 22:05:24 +09:00
parent 8158c9bbc3
commit 13f2c6d089
2 changed files with 66 additions and 23 deletions

View File

@ -8,6 +8,8 @@ import {
_LOGGER_CONFIG, _LOGGER_CONFIG,
} from '../core'; } from '../core';
export type ConsoleFunc = (message?: any, ...optionalParams: any[]) => void;
@Injectable() @Injectable()
export class LoggerService { export class LoggerService {
private _isIE: boolean; private _isIE: boolean;
@ -18,30 +20,59 @@ export class LoggerService {
) { ) {
this._isIE = isPlatformBrowser(platformId) && this._isIE = isPlatformBrowser(platformId) &&
!!(navigator.userAgent.indexOf('MSIE') !== -1 || navigator.userAgent.match(/Trident\//) || navigator.userAgent.match(/Edge\//)); !!(navigator.userAgent.indexOf('MSIE') !== -1 || navigator.userAgent.match(/Trident\//) || navigator.userAgent.match(/Edge\//));
} }
public trace(message, ...additional: any[]): void { public get trace(): ConsoleFunc {
this._log(LoggerLevel.TRACE, message, additional); 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 { private _console_log: ConsoleFunc;
this._log(LoggerLevel.DEBUG, message, additional);
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 { private getLogHeader(level: LoggerLevel): any[] {
this._log(LoggerLevel.INFO, message, additional); const params: any[] = [];
} params.push(`%c${this._timestamp()} [${LoggerLevelName[level]}]`);
if (!this._isIE) {
public log(message, ...additional: any[]): void { const color = this._getColor(level);
this._log(LoggerLevel.LOG, message, additional); params.push(`color:${color}`);
} }
return params;
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 _timestamp(): string { private _timestamp(): string {
@ -77,22 +108,33 @@ export class LoggerService {
const color = this._getColor(level); 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 { 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) { switch (level) {
case LoggerLevel.WARN: case LoggerLevel.WARN:
console.warn(`${this._timestamp()} [${LoggerLevelName[level]}] `, message, ...additional); console.warn.apply(console, params);
break; break;
case LoggerLevel.ERROR: case LoggerLevel.ERROR:
console.error(`${this._timestamp()} [${LoggerLevelName[level]}] `, message, ...additional); console.error.apply(console, params);
break; break;
case LoggerLevel.INFO: case LoggerLevel.INFO:
console.log(`${this._timestamp()} [${LoggerLevelName[level]}] `, message, ...additional); console.info.apply(console, params);
break; break;
default: default:
console.log(`${this._timestamp()} [${LoggerLevelName[level]}] `, message, ...additional); console.log.apply(console, params);
} }
} }

View File

@ -20,6 +20,7 @@ export class DiscoverySubscriber {
private store: Store<DiscoverStore.State>, private store: Store<DiscoverStore.State>,
private loggerService: LoggerService, private loggerService: LoggerService,
) { ) {
this.loggerService.debug('dddddd');
} }
@RPCSubscriber({method: 'DiscoveryService.discoveredZone'}) @RPCSubscriber({method: 'DiscoveryService.discoveredZone'})