This commit is contained in:
crusader 2018-03-25 19:34:03 +09:00
parent 6edb85aa07
commit 520470fd93
14 changed files with 272 additions and 10 deletions

View File

@ -0,0 +1,6 @@
import { LoggerLevel } from './type';
export interface LoggerConfig {
level: LoggerLevel;
serverLogLevel?: LoggerLevel;
}

View File

@ -0,0 +1,3 @@
export * from './config';
export * from './token';
export * from './type';

View File

@ -0,0 +1,3 @@
import { InjectionToken } from '@angular/core';
export const _LOGGER_CONFIG = new InjectionToken('@loafer/ng-logger Internal Logger config');

View File

@ -0,0 +1,26 @@
export enum LoggerLevel {
TRACE = 0,
DEBUG,
INFO,
LOG,
WARN,
ERROR,
OFF,
}
export const LoggerLevelName = [
'TRACE',
'DEBUG',
'INFO',
'LOG',
'WARN',
'ERROR',
'OFF'
];
export interface ServerLoggingParameter {
level: string;
message: string;
addtional?: string;
timestamp: Date;
}

View File

@ -0,0 +1 @@
export * from './ng-logger.module';

View File

@ -0,0 +1,65 @@
import {
NgModule,
ModuleWithProviders,
Type,
Inject,
InjectionToken,
} from '@angular/core';
import {
_LOGGER_CONFIG,
LoggerConfig,
} from './core';
import {
SERVICES, LoggerService,
} from './service';
export interface LoggerFeatureModuleConfig {
url?: any;
}
export interface LoggerRootModuleConfig {
config: LoggerConfig;
}
@NgModule({})
export class LoggerRootModule {
constructor(
private loggerService: LoggerService,
) {
}
}
@NgModule({})
export class LoggerFeatureModule {
constructor(
private loggerService: LoggerService,
private root: LoggerRootModule,
) {
}
}
@NgModule({})
export class LoggerModule {
static forRoot(config: LoggerRootModuleConfig): ModuleWithProviders {
return {
ngModule: LoggerRootModule,
providers: [
{
provide: _LOGGER_CONFIG,
useValue: config.config,
},
SERVICES,
],
};
}
static forFeature(config: LoggerFeatureModuleConfig): ModuleWithProviders {
return {
ngModule: LoggerFeatureModule,
providers: [
],
};
}
}

View File

@ -0,0 +1,7 @@
export * from './logger.service';
import { LoggerService } from './logger.service';
export const SERVICES = [
LoggerService,
];

View File

@ -0,0 +1,116 @@
import { Injectable, Inject, PLATFORM_ID } from '@angular/core';
import { isPlatformBrowser } from '@angular/common';
import {
LoggerConfig,
LoggerLevel,
LoggerLevelName,
_LOGGER_CONFIG,
} from '../core';
@Injectable()
export class LoggerService {
private _isIE: boolean;
public constructor(
@Inject(_LOGGER_CONFIG) private readonly config: LoggerConfig,
@Inject(PLATFORM_ID) private readonly platformId,
) {
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 debug(message, ...additional: any[]): void {
this._log(LoggerLevel.DEBUG, message, additional);
}
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 _timestamp(): string {
return new Date().toISOString();
}
private _log(level: LoggerLevel, message, additional: any[] = []): void {
if (!message) {
return;
}
// Allow logging on server even if client log level is off
// if (logOnServer) {
// this._logOnServer(level, message, additional);
// }
// if no message or the log level is less than the environ
if (level < this.config.level) {
return;
}
try {
message = typeof message === 'string' ? message : JSON.stringify(message, null, 2);
} catch (e) {
additional = [message, ...additional];
message = 'The provided "message" value could not be parsed with JSON.stringify().';
}
// Coloring doesn't work in IE
if (this._isIE) {
return this._logIE(level, message, additional);
}
const color = this._getColor(level);
console.log(`%c${this._timestamp()} [${LoggerLevelName[level]}]`, `color:${color}`, message, ...additional);
}
private _logIE(level: LoggerLevel, message: string, additional: any[] = []): void {
switch (level) {
case LoggerLevel.WARN:
console.warn(`${this._timestamp()} [${LoggerLevelName[level]}] `, message, ...additional);
break;
case LoggerLevel.ERROR:
console.error(`${this._timestamp()} [${LoggerLevelName[level]}] `, message, ...additional);
break;
case LoggerLevel.INFO:
console.log(`${this._timestamp()} [${LoggerLevelName[level]}] `, message, ...additional);
break;
default:
console.log(`${this._timestamp()} [${LoggerLevelName[level]}] `, message, ...additional);
}
}
private _getColor(level: LoggerLevel): 'blue' | 'teal' | 'gray' | 'red' | undefined {
switch (level) {
case LoggerLevel.TRACE:
return 'blue';
case LoggerLevel.DEBUG:
return 'teal';
case LoggerLevel.INFO:
case LoggerLevel.LOG:
return 'gray';
case LoggerLevel.WARN:
case LoggerLevel.ERROR:
return 'red';
case LoggerLevel.OFF:
default:
return;
}
}
}

View File

@ -62,7 +62,3 @@ export class RESTModule {
};
}
}
export function createSourceInstances(...instances: any[]) {
return instances;
}

View File

@ -0,0 +1,22 @@
import { NgModule } from '@angular/core';
import { LoggerModule } from '@loafer/ng-logger';
import { LoggerLevel } from '@loafer/ng-logger/core';
import { environment } from '../environments/environment';
@NgModule({
exports: [
LoggerModule,
],
imports: [
LoggerModule.forRoot({
config: {
level: LoggerLevel.DEBUG,
},
}),
],
providers: [
]
})
export class AppLoggerModule { }

View File

@ -14,6 +14,7 @@ import { AppStoreModule } from './app-store.module';
import { AppL10NModule } from './app-l10n.module';
import { AppRPCModule } from './app-rpc.module';
import { AppRESTModule } from './app-rest.module';
import { AppLoggerModule } from './app-logger.module';
import { MaterialModule } from 'packages/commons/material/material.module';
@ -25,14 +26,16 @@ import { AuthGuard } from './commons/guard/auth.guard';
imports: [
BrowserModule,
BrowserAnimationsModule,
MaterialModule,
HttpClientModule,
MemberModule,
AppRoutingModule,
AppStoreModule,
AppL10NModule,
AppRPCModule,
AppRESTModule,
MaterialModule,
HttpClientModule,
MemberModule,
AppLoggerModule,
],
providers: [
CookieService,

View File

@ -0,0 +1,10 @@
import { NgModule } from '@angular/core';
import { LoggerModule } from '@loafer/ng-logger';
@NgModule({
imports: [
LoggerModule.forFeature({}),
],
})
export class DiscoveryLoggerModule { }

View File

@ -8,6 +8,7 @@ import { InputChipModule } from 'packages/commons/component/input-chip/input-chi
import { DiscoveryStoreModule } from './discovery-store.module';
import { DiscoveryRPCModule } from './discovery-rpc.module';
import { DiscoveryLoggerModule } from './discovery-logger.module';
import { COMPONENTS } from './component';
import { SERVICES } from './service';
@ -19,9 +20,10 @@ import { TreeModule } from 'angular-tree-component';
MaterialModule,
InputChipModule,
FormsModule,
TreeModule,
DiscoveryStoreModule,
DiscoveryRPCModule,
TreeModule
DiscoveryLoggerModule,
],
declarations: [
COMPONENTS

View File

@ -2,6 +2,7 @@ import { Injectable } from '@angular/core';
import { Store, select } from '@ngrx/store';
import { RPCSubscriber } from '@loafer/ng-rpc/decorator';
import { LoggerService } from '@loafer/ng-logger/service';
import * as DiscoverStore from '../store/discover';
@ -17,18 +18,19 @@ export class DiscoverySubscriber {
public constructor(
private store: Store<DiscoverStore.State>,
private loggerService: LoggerService,
) {
}
@RPCSubscriber({method: 'DiscoveryService.discoveredZone'})
public discoveredZone(zone: Zone): void {
console.log(`DiscoverySubscriber.discoveredZone zone:${zone}`);
this.loggerService.debug('DiscoverySubscriber.discoveredZone zone:', zone);
this.store.dispatch(new DiscoverStore.DiscoveredZone(zone));
}
@RPCSubscriber({method: 'DiscoveryService.discoveredHost'})
public discoveredHost(host: Host): void {
console.log(`DiscoverySubscriber.discoveredHost host:${host}`);
this.loggerService.debug('DiscoverySubscriber.discoveredHost host:', host);
this.store.dispatch(new DiscoverStore.DiscoveredHost(host));
}