Skip to content

Commit 2374cce

Browse files
committed
feat(decorators): add LogExecution decorator for method logging
- Implemented LogExecution decorator to automatically log method entry, exit, and results. - Integrated with the Logger class to provide detailed logs including context and arguments. - Enhanced debugging and monitoring capabilities with method-level logging.
1 parent 738d4b0 commit 2374cce

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

src/decorators/Logger.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { Logger } from "../config/logger"; // Adjust the import path as necessary
2+
3+
// Initialize the logger
4+
const logger = new Logger({
5+
file: "application.log",
6+
level: "info",
7+
timestampFormat: "locale",
8+
});
9+
export function LogExecution() {
10+
return function (
11+
target: any,
12+
propertyKey: string,
13+
descriptor: PropertyDescriptor
14+
) {
15+
const originalMethod = descriptor.value;
16+
17+
descriptor.value = async function (...args: any[]) {
18+
const methodName = propertyKey;
19+
const className = target.constructor.name;
20+
const context = (this as any)?.ctx || args[0];
21+
// Log method entry
22+
logger.info(`Entering ${className}.`, `${methodName}`, { context, args });
23+
24+
const result = await originalMethod.apply(this, args);
25+
26+
// Log method exit
27+
logger.info(`Exiting ${className}.`, `${methodName}`, {
28+
context,
29+
result,
30+
});
31+
return result;
32+
};
33+
34+
return descriptor;
35+
};
36+
}

0 commit comments

Comments
 (0)