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,
} 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);
}
}

View File

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