Files
cnpmcore/app/common/aop/AsyncTimer.ts
fengmk2 ffe723e65f refactor: use oxlint instead of eslint (#772)
say goodbye to eslint
2025-03-13 23:31:13 +08:00

34 lines
842 B
TypeScript

import { performance } from 'node:perf_hooks';
import type { AdviceContext, IAdvice } from '@eggjs/tegg/aop';
import { Advice } from '@eggjs/tegg/aop';
import { Inject } from '@eggjs/tegg';
import type { EggLogger } from 'egg';
// auto print async function call performance timer log into logger
@Advice()
export class AsyncTimer implements IAdvice {
@Inject()
private readonly logger: EggLogger;
private start: number;
private succeed = true;
async beforeCall() {
this.start = performance.now();
}
async afterThrow() {
this.succeed = false;
}
async afterFinally(ctx: AdviceContext) {
const ms = Math.floor((performance.now() - this.start) * 1000) / 1000;
this.logger.info(
'[%s] [%s:%s|%s]',
ms,
ctx.that.constructor.name,
ctx.method,
this.succeed ? 'T' : 'F'
);
}
}