diff --git a/src/@loafer/ng-logger/core/config.ts b/src/@loafer/ng-logger/core/config.ts new file mode 100644 index 0000000..0e2c64d --- /dev/null +++ b/src/@loafer/ng-logger/core/config.ts @@ -0,0 +1,6 @@ +import { LoggerLevel } from './type'; + +export interface LoggerConfig { + level: LoggerLevel; + serverLogLevel?: LoggerLevel; +} diff --git a/src/@loafer/ng-logger/core/index.ts b/src/@loafer/ng-logger/core/index.ts new file mode 100644 index 0000000..40a47eb --- /dev/null +++ b/src/@loafer/ng-logger/core/index.ts @@ -0,0 +1,3 @@ +export * from './config'; +export * from './token'; +export * from './type'; diff --git a/src/@loafer/ng-logger/core/token.ts b/src/@loafer/ng-logger/core/token.ts new file mode 100644 index 0000000..ff2a17c --- /dev/null +++ b/src/@loafer/ng-logger/core/token.ts @@ -0,0 +1,3 @@ +import { InjectionToken } from '@angular/core'; + +export const _LOGGER_CONFIG = new InjectionToken('@loafer/ng-logger Internal Logger config'); diff --git a/src/@loafer/ng-logger/core/type.ts b/src/@loafer/ng-logger/core/type.ts new file mode 100644 index 0000000..72d4473 --- /dev/null +++ b/src/@loafer/ng-logger/core/type.ts @@ -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; +} diff --git a/src/@loafer/ng-logger/index.ts b/src/@loafer/ng-logger/index.ts new file mode 100644 index 0000000..4d007cd --- /dev/null +++ b/src/@loafer/ng-logger/index.ts @@ -0,0 +1 @@ +export * from './ng-logger.module'; diff --git a/src/@loafer/ng-logger/ng-logger.module.ts b/src/@loafer/ng-logger/ng-logger.module.ts new file mode 100644 index 0000000..bcde9f1 --- /dev/null +++ b/src/@loafer/ng-logger/ng-logger.module.ts @@ -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: [ + ], + }; + } +} diff --git a/src/@loafer/ng-logger/service/index.ts b/src/@loafer/ng-logger/service/index.ts new file mode 100644 index 0000000..663327c --- /dev/null +++ b/src/@loafer/ng-logger/service/index.ts @@ -0,0 +1,7 @@ +export * from './logger.service'; + +import { LoggerService } from './logger.service'; + +export const SERVICES = [ + LoggerService, +]; diff --git a/src/@loafer/ng-logger/service/logger.service.ts b/src/@loafer/ng-logger/service/logger.service.ts new file mode 100644 index 0000000..622c564 --- /dev/null +++ b/src/@loafer/ng-logger/service/logger.service.ts @@ -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; + } + } +} diff --git a/src/@loafer/ng-rest/ng-rest.module.ts b/src/@loafer/ng-rest/ng-rest.module.ts index 9ce3f62..0da03a8 100644 --- a/src/@loafer/ng-rest/ng-rest.module.ts +++ b/src/@loafer/ng-rest/ng-rest.module.ts @@ -62,7 +62,3 @@ export class RESTModule { }; } } - -export function createSourceInstances(...instances: any[]) { - return instances; -} diff --git a/src/app/app-logger.module.ts b/src/app/app-logger.module.ts new file mode 100644 index 0000000..1bd7f9a --- /dev/null +++ b/src/app/app-logger.module.ts @@ -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 { } diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 4ed89c1..c36bf3a 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -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, diff --git a/src/packages/discovery/discovery-logger.module.ts b/src/packages/discovery/discovery-logger.module.ts new file mode 100644 index 0000000..2431e36 --- /dev/null +++ b/src/packages/discovery/discovery-logger.module.ts @@ -0,0 +1,10 @@ +import { NgModule } from '@angular/core'; +import { LoggerModule } from '@loafer/ng-logger'; + + +@NgModule({ + imports: [ + LoggerModule.forFeature({}), + ], +}) +export class DiscoveryLoggerModule { } diff --git a/src/packages/discovery/discovery.module.ts b/src/packages/discovery/discovery.module.ts index bff106b..d6800eb 100644 --- a/src/packages/discovery/discovery.module.ts +++ b/src/packages/discovery/discovery.module.ts @@ -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 diff --git a/src/packages/discovery/subscriber/discovery.subscriber.ts b/src/packages/discovery/subscriber/discovery.subscriber.ts index 7149a4d..e1b6898 100644 --- a/src/packages/discovery/subscriber/discovery.subscriber.ts +++ b/src/packages/discovery/subscriber/discovery.subscriber.ts @@ -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, + 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)); }