function LogMethodExecution(target: any, propertyName: string, descriptor: TypedPropertyDescriptor<Function>) {
const originalMethod = descriptor.value;
descriptor.value = function (...args: any[]) {
console.log(`Calling ${propertyName}`);
const result = originalMethod.apply(this, args);
if (result instanceof Promise) {
result.then(() => console.log(`${propertyName} finished execution`))
.catch((error) => console.log(`Error in ${propertyName}: ${error}`));
} else {
console.log(`${propertyName} finished execution`);
}
return result;
}
}
function ApplyLogToMethods(target: Function) {
for (const propertyName of Object.getOwnPropertyNames(target.prototype)) {
const descriptor = Object.getOwnPropertyDescriptor(target.prototype, propertyName);
if (descriptor && typeof descriptor.value === 'function') {
Object.defineProperty(target.prototype, propertyName, {
value: LogMethodExecution(target, propertyName, descriptor)
});
}
}
}
@ApplyLogToMethods
class YourService {
// Your methods...
}