@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME) public @interface MetricTime { String value() default ""; }
@Aspect @Component public class MetricAspect { // 在执行MailService的每个方法前后执行: @Around("@annotation(metricTime)") public Object doLogging(ProceedingJoinPoint pjp, MetricTime metricTime) throws Throwable { System.err.println("[Metrics] start " + pjp.getSignature()); String methodName = metricTime.value(); long start = System.currentTimeMillis(); try { return pjp.proceed(); } finally { long t = System.currentTimeMillis() - start; // 写入日志或发送至JMX: System.err.println("[Metrics] " + methodName + ": " + t + "ms"); } } }
运行日志
设置编码:utf-8
LogFilter: process /signin
luo@123.com
[Before] do access check...
[Metrics] start User com.service.UserService.login(String,String)
select id,name from user where email=? and password=? limit 1
Hi, luo1! You are logged in at 2021-09-18T09:47:59.0066418-04:00[America/New_York]
[Metrics] login: 146ms
Sign in to make a reply
净净一隅
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MetricTime {
String value() default "";
}
@Aspect
@Component
public class MetricAspect {
// 在执行MailService的每个方法前后执行:
@Around("@annotation(metricTime)")
public Object doLogging(ProceedingJoinPoint pjp, MetricTime metricTime) throws Throwable {
System.err.println("[Metrics] start " + pjp.getSignature());
String methodName = metricTime.value();
long start = System.currentTimeMillis();
try {
return pjp.proceed();
} finally {
long t = System.currentTimeMillis() - start;
// 写入日志或发送至JMX:
System.err.println("[Metrics] " + methodName + ": " + t + "ms");
}
}
}
运行日志
设置编码:utf-8
LogFilter: process /signin
luo@123.com
[Before] do access check...
[Metrics] start User com.service.UserService.login(String,String)
select id,name from user where email=? and password=? limit 1
Hi, luo1! You are logged in at 2021-09-18T09:47:59.0066418-04:00[America/New_York]
[Metrics] login: 146ms