tprompter
Version:
```bash $ ask anything ```
31 lines (30 loc) • 1.23 kB
JavaScript
import { Container } from 'typedi';
import { LoggerService } from '../logger/index.js';
export function logDuration() {
return function (target, propertyKey, descriptor) {
const rootLogger = Container.get(LoggerService);
const logger = rootLogger.child('timings');
const originalMethod = descriptor.value;
descriptor.value = function (...args) {
const start = Date.now();
const result = originalMethod.apply(this, args);
if (result instanceof Promise) {
return result
.then((res) => {
const duration = Date.now() - start;
logger.debug(`Execution of ${propertyKey} took ${duration}ms`);
return res;
})
.catch((err) => {
const duration = Date.now() - start;
logger.debug(`Execution of ${propertyKey} took ${duration}ms`);
throw err;
});
}
const duration = Date.now() - start;
logger.debug(`Execution of ${propertyKey} took ${duration}ms`);
return result;
};
return descriptor;
};
}