ing
This commit is contained in:
parent
960e6645f6
commit
634aa17885
6
src/commons/logger/core/config.ts
Normal file
6
src/commons/logger/core/config.ts
Normal file
|
@ -0,0 +1,6 @@
|
|||
import { LoggerLevel } from './type';
|
||||
|
||||
export class LoggerConfig {
|
||||
level: LoggerLevel;
|
||||
serverLogLevel: LoggerLevel;
|
||||
}
|
3
src/commons/logger/core/index.ts
Normal file
3
src/commons/logger/core/index.ts
Normal file
|
@ -0,0 +1,3 @@
|
|||
export * from './config';
|
||||
export * from './token';
|
||||
export * from './type';
|
4
src/commons/logger/core/token.ts
Normal file
4
src/commons/logger/core/token.ts
Normal file
|
@ -0,0 +1,4 @@
|
|||
import { Inject, Injectable, InjectionToken } from '@angular/core';
|
||||
import { LoggerLevel } from './type';
|
||||
|
||||
export const LOGGER_LEVEL = new InjectionToken<LoggerLevel>('LoggerLevel');
|
26
src/commons/logger/core/type.ts
Normal file
26
src/commons/logger/core/type.ts
Normal 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;
|
||||
}
|
39
src/commons/logger/logger.module.ts
Normal file
39
src/commons/logger/logger.module.ts
Normal file
|
@ -0,0 +1,39 @@
|
|||
import {
|
||||
NgModule,
|
||||
ModuleWithProviders,
|
||||
} from '@angular/core';
|
||||
import { CommonModule } from '@angular/common';
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@NgModule({
|
||||
})
|
||||
export class LoggerRootModule { }
|
||||
|
||||
@NgModule({
|
||||
})
|
||||
export class LoggerFeatureModule { }
|
||||
|
||||
@NgModule({
|
||||
})
|
||||
export class LoggerModule {
|
||||
static forRoot(reducers: any): ModuleWithProviders {
|
||||
return {
|
||||
ngModule: LoggerRootModule,
|
||||
providers: [
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
static forFeature(featureName: string, reducers?: any): ModuleWithProviders {
|
||||
return {
|
||||
ngModule: LoggerFeatureModule,
|
||||
providers: [
|
||||
],
|
||||
};
|
||||
}
|
||||
}
|
93
src/commons/logger/service/logger.service.ts
Normal file
93
src/commons/logger/service/logger.service.ts
Normal file
|
@ -0,0 +1,93 @@
|
|||
import {
|
||||
LoggerConfig,
|
||||
LoggerLevel,
|
||||
} from '../core';
|
||||
|
||||
|
||||
export abstract class LoggerService {
|
||||
|
||||
public constructor(
|
||||
private readonly config: LoggerConfig
|
||||
) {
|
||||
|
||||
}
|
||||
|
||||
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, logOnServer: boolean, 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()} [${Levels[level]}]`, `color:${color}`, 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;
|
||||
}
|
||||
}
|
||||
}
|
20
src/commons/logger/store/logger.action.ts
Normal file
20
src/commons/logger/store/logger.action.ts
Normal file
|
@ -0,0 +1,20 @@
|
|||
import { Action } from '@ngrx/store';
|
||||
|
||||
import {
|
||||
LoggerLevel,
|
||||
ServerLoggingParameter,
|
||||
} from '../core';
|
||||
|
||||
export enum ActionType {
|
||||
ServerLogging = '[@@LOGGER] ServerLogging',
|
||||
}
|
||||
|
||||
export class ServerLogging implements Action {
|
||||
readonly type = ActionType.ServerLogging;
|
||||
|
||||
constructor(public payload: ServerLoggingParameter) {}
|
||||
}
|
||||
|
||||
export type Actions =
|
||||
| ServerLogging
|
||||
;
|
|
@ -10,13 +10,13 @@ import {
|
|||
|
||||
export interface ClientNotification {
|
||||
method: string;
|
||||
params?: any[];
|
||||
params?: string[];
|
||||
}
|
||||
|
||||
export interface ClientRequest {
|
||||
jsonrpc: string;
|
||||
method: string;
|
||||
params?: any[];
|
||||
params?: string[];
|
||||
id?: number;
|
||||
}
|
||||
|
||||
|
@ -29,10 +29,12 @@ export interface ClientResponse {
|
|||
|
||||
export class RPCClientJSONCodec extends RPCClientCodec {
|
||||
public request(method: string, args: any[], id?: number): any {
|
||||
const params = convertParamsToStringArray(args);
|
||||
|
||||
const req: ClientRequest = {
|
||||
jsonrpc: '2.0',
|
||||
method: method,
|
||||
params: args,
|
||||
params: 0 === params.length ? null : params,
|
||||
id: id,
|
||||
};
|
||||
return JSON.stringify(req);
|
||||
|
@ -48,6 +50,35 @@ export class RPCClientJSONCodec extends RPCClientCodec {
|
|||
}
|
||||
}
|
||||
|
||||
function convertParamsToStringArray(args: any[]): string[] | undefined {
|
||||
const values: string[] = [];
|
||||
|
||||
if (undefined === args || null === args || 0 === args.length) {
|
||||
return values;
|
||||
}
|
||||
|
||||
for (let indexI = 0; indexI < args.length; indexI++) {
|
||||
const arg = args[indexI];
|
||||
|
||||
switch (typeof arg) {
|
||||
case 'boolean':
|
||||
case 'number': // enum
|
||||
values.push(String(arg));
|
||||
break;
|
||||
case 'string':
|
||||
values.push(arg);
|
||||
break;
|
||||
case 'object': // array, map
|
||||
values.push(JSON.stringify(arg));
|
||||
break;
|
||||
default:
|
||||
throw new Error(`Not supported type[${typeof arg}]`);
|
||||
}
|
||||
}
|
||||
|
||||
return values;
|
||||
}
|
||||
|
||||
export class RPCClientJSONResponseCodec extends RPCClientResponseCodec {
|
||||
public constructor(private _res: ClientResponse) {
|
||||
super();
|
||||
|
|
Loading…
Reference in New Issue
Block a user