refactor(logging): 重构执行时间统计功能的注解和切面类命名
This commit is contained in:
+1
-1
@@ -31,7 +31,7 @@ import java.lang.annotation.Target;
|
|||||||
@Documented
|
@Documented
|
||||||
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
|
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
public @interface ExecutionTimeStatisticsFace {
|
public @interface ExecutionTimeLog {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 接口描述
|
* 接口描述
|
||||||
+6
-6
@@ -32,20 +32,20 @@ import org.springframework.stereotype.Component;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
@Aspect
|
@Aspect
|
||||||
@Component
|
@Component
|
||||||
public class ExecutionTimeStatisticsFaceAspect {
|
public class ExecutionTimeLogAspect {
|
||||||
|
|
||||||
@Around("@annotation(executionTimeStatisticsFace)")
|
@Around("@annotation(executionTimeLog)")
|
||||||
public Object aroundFace(ProceedingJoinPoint joinPoint, ExecutionTimeStatisticsFace executionTimeStatisticsFace)
|
public Object aroundFace(ProceedingJoinPoint joinPoint, ExecutionTimeLog executionTimeLog)
|
||||||
throws Throwable {
|
throws Throwable {
|
||||||
TimeInterval timeInterval = new TimeInterval();
|
TimeInterval timeInterval = new TimeInterval();
|
||||||
try {
|
try {
|
||||||
Object proceed = joinPoint.proceed();
|
Object proceed = joinPoint.proceed();
|
||||||
log.info("[{}] 接口执行成功,耗时: {} ms", executionTimeStatisticsFace.description(),
|
log.info("[{}] 接口执行成功,耗时: {} ms", executionTimeLog.description(),
|
||||||
timeInterval.intervalMs());
|
timeInterval.intervalMs());
|
||||||
return proceed;
|
return proceed;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
if (executionTimeStatisticsFace.showErrLog()) {
|
if (executionTimeLog.showErrLog()) {
|
||||||
log.error("[{}] 接口执行失败,耗时: {} ms", executionTimeStatisticsFace.description(),
|
log.error("[{}] 接口执行失败,耗时: {} ms", executionTimeLog.description(),
|
||||||
timeInterval.intervalMs(), e);
|
timeInterval.intervalMs(), e);
|
||||||
}
|
}
|
||||||
throw e;
|
throw e;
|
||||||
+45
-45
@@ -1,45 +1,45 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2021-2026 Odboy
|
* Copyright 2021-2026 Odboy
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
* You may obtain a copy of the License at
|
* You may obtain a copy of the License at
|
||||||
*
|
*
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
*
|
*
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package cn.odboy.framework.logging;
|
package cn.odboy.framework.logging;
|
||||||
|
|
||||||
import java.lang.annotation.Documented;
|
import java.lang.annotation.Documented;
|
||||||
import java.lang.annotation.ElementType;
|
import java.lang.annotation.ElementType;
|
||||||
import java.lang.annotation.Inherited;
|
import java.lang.annotation.Inherited;
|
||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
||||||
import java.lang.annotation.RetentionPolicy;
|
import java.lang.annotation.RetentionPolicy;
|
||||||
import java.lang.annotation.Target;
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 统计执行时长(仅对代理方法有效)
|
* 统计执行时长(仅对代理方法有效)
|
||||||
*
|
*
|
||||||
* @author odboy
|
* @author odboy
|
||||||
*/
|
*/
|
||||||
@Inherited
|
@Inherited
|
||||||
@Documented
|
@Documented
|
||||||
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
|
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
public @interface ExecutionTimeStatisticsFace {
|
public @interface ExecutionTimeLog {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 接口描述
|
* 接口描述
|
||||||
*/
|
*/
|
||||||
String description();
|
String description();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 是否展示异常日志
|
* 是否展示异常日志
|
||||||
*/
|
*/
|
||||||
boolean showErrLog() default true;
|
boolean showErrLog() default true;
|
||||||
}
|
}
|
||||||
+54
-54
@@ -1,54 +1,54 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2021-2026 Odboy
|
* Copyright 2021-2026 Odboy
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
* You may obtain a copy of the License at
|
* You may obtain a copy of the License at
|
||||||
*
|
*
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
*
|
*
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package cn.odboy.framework.logging;
|
package cn.odboy.framework.logging;
|
||||||
|
|
||||||
import cn.hutool.core.date.TimeInterval;
|
import cn.hutool.core.date.TimeInterval;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.aspectj.lang.ProceedingJoinPoint;
|
import org.aspectj.lang.ProceedingJoinPoint;
|
||||||
import org.aspectj.lang.annotation.Around;
|
import org.aspectj.lang.annotation.Around;
|
||||||
import org.aspectj.lang.annotation.Aspect;
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 执行时间统计
|
* 执行时间统计
|
||||||
*
|
*
|
||||||
* @author odboy
|
* @author odboy
|
||||||
* @date 2025-05-07
|
* @date 2025-05-07
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Aspect
|
@Aspect
|
||||||
@Component
|
@Component
|
||||||
public class ExecutionTimeStatisticsFaceAspect {
|
public class ExecutionTimeLogAspect {
|
||||||
|
|
||||||
@Around("@annotation(executionTimeStatisticsFace)")
|
@Around("@annotation(executionTimeLog)")
|
||||||
public Object aroundFace(ProceedingJoinPoint joinPoint, ExecutionTimeStatisticsFace executionTimeStatisticsFace)
|
public Object aroundFace(ProceedingJoinPoint joinPoint, ExecutionTimeLog executionTimeLog)
|
||||||
throws Throwable {
|
throws Throwable {
|
||||||
TimeInterval timeInterval = new TimeInterval();
|
TimeInterval timeInterval = new TimeInterval();
|
||||||
try {
|
try {
|
||||||
Object proceed = joinPoint.proceed();
|
Object proceed = joinPoint.proceed();
|
||||||
log.info("[{}] 接口执行成功,耗时: {} ms", executionTimeStatisticsFace.description(),
|
log.info("[{}] 接口执行成功,耗时: {} ms", executionTimeLog.description(),
|
||||||
timeInterval.intervalMs());
|
timeInterval.intervalMs());
|
||||||
return proceed;
|
return proceed;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
if (executionTimeStatisticsFace.showErrLog()) {
|
if (executionTimeLog.showErrLog()) {
|
||||||
log.error("[{}] 接口执行失败,耗时: {} ms", executionTimeStatisticsFace.description(),
|
log.error("[{}] 接口执行失败,耗时: {} ms", executionTimeLog.description(),
|
||||||
timeInterval.intervalMs(), e);
|
timeInterval.intervalMs(), e);
|
||||||
}
|
}
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user