Logging disable on certain Environments
vjhurani opened this issue · 0 comments
vjhurani commented
Now I have added Wove to all the classes in the project. But I want the @before / @ after only work in DEV env and not Prod Env,
Can i configure it? Please guide.
import { Injectable } from '@angular/core';
import { LogService } from '../../shared/services/log.service';
import {
beforeMethod,
afterMethod,
Metadata,
} from 'aspect.js';
@Injectable()
export class LoggingAspect {
public static loggerService: LogService;
beforeMessage = '';
afterMessage = '';
constructor() {
}
@beforeMethod({
classNamePattern: /^[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*$/,
methodNamePattern: /^[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*$/
})
before(meta: Metadata) {
// tslint:disable-next-line:max-line-length
this.beforeMessage = 'Aspect Logger @BeforeMethod - ' + this.getMetaInformation(meta);
console.log(this.beforeMessage);
LoggingAspect.loggerService.postInfoMessage(this.beforeMessage);
}
@afterMethod({
classNamePattern: /^[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*$/,
methodNamePattern: /^[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*$/
})
after(meta: Metadata) {
// tslint:disable-next-line:max-line-length
this.afterMessage = 'Aspect Logger @AfterMethod - ' + this.getMetaInformation(meta);
console.log(this.afterMessage);
LoggingAspect.loggerService.postInfoMessage(this.afterMessage);
}
getMetaInformation(meta: Metadata): string {
return `Class : ${meta.className}, Method : ${meta.method.name}, Arguments ${this.getArgumentValues(meta.method.args)}`;
}
getArgumentValues(argumentsPassed: any[]) {
let values = 'No arguments';
if ( argumentsPassed !== null && argumentsPassed !== undefined && argumentsPassed.length > 0) {
values = '';
for ( let arg = 0; arg < argumentsPassed.length; ++ arg) {
const argument = argumentsPassed[arg];
if (Array.isArray(argument)) {
for (let i = 0; i < argument.length; ++ i) {
const element = argument[i];
values += `: ${this.getValues(element)} `;
}
} else {
values += `: ${this.getValues(argument)} `;
}
}
}
return values;
}
getValues(argument) {
if ( argument !== null && argument !== undefined) {
if ( this.checkIfObject(argument)) {
return Object.entries(argument);
} else if (this.checkIfPrimitive(argument)) {
}
return argument;
}
}
checkIfPrimitive(argument) {
return ( typeof(argument) === 'string' || 'number' || 'boolean' || 'undefined' );
}
checkIfObject(argument) {
return ( typeof(argument) === 'object' );
}
}