1. import org.springframework.boot.logging.LogLevel;
    2. import java.lang.annotation.*;
    3. /**
    4. * 添加该注解,则添加该注解的方法的入参或者出参会自动打印
    5. *
    6. * @author shizi
    7. * @since 2019/12/24 9:56 下午
    8. */
    9. @Documented
    10. @Target({ElementType.METHOD, ElementType.TYPE})
    11. @Retention(RetentionPolicy.RUNTIME)
    12. public @interface EnableAopLog {
    13. /**
    14. * 是否打印请求
    15. * @return 默认不打印
    16. */
    17. boolean printRequest() default false;
    18. /**
    19. * 如果打印的话,则打印的级别是什么
    20. * @return 默认为debug
    21. */
    22. LogLevel logLevel() default LogLevel.DEBUG;
    23. /**
    24. * 是否打印返回值
    25. * @return 默认不打印
    26. */
    27. boolean printResponse() default false;
    28. }

    解析部分:

    1. import com.alibaba.fastjson.JSON;
    2. import com.isyscore.iop.panda.exception.BusinessException;
    3. import com.isyscore.isc.neo.util.TimeRangeStrUtil;
    4. import lombok.extern.slf4j.Slf4j;
    5. import org.aspectj.lang.ProceedingJoinPoint;
    6. import org.aspectj.lang.Signature;
    7. import org.aspectj.lang.annotation.Around;
    8. import org.aspectj.lang.annotation.Aspect;
    9. import org.aspectj.lang.reflect.MethodSignature;
    10. import org.springframework.boot.logging.LogLevel;
    11. import org.springframework.stereotype.Component;
    12. import java.lang.reflect.Method;
    13. import java.util.HashMap;
    14. import static com.isyscore.iop.panda.constant.AppConstant.LOG_PRE;
    15. /**
    16. * @author robot
    17. */
    18. @Slf4j
    19. @Aspect
    20. @Component
    21. public class ControllerAop {
    22. /**
    23. * 拦截方法中添加注解{@link EnableAopLog}的类和方法
    24. */
    25. @Around("@annotation(com.isyscore.iop.panda.annotation.EnableAopLog) || @within(com.isyscore.iop.panda.annotation.EnableAopLog)")
    26. public Object aroundParamFun1(ProceedingJoinPoint pjp) throws Throwable {
    27. long start = System.currentTimeMillis();
    28. HashMap<String, Object> outInfo = new HashMap<>();
    29. Signature sig = pjp.getSignature();
    30. MethodSignature methodSignature = (MethodSignature) sig;
    31. Method currentMethod;
    32. try {
    33. currentMethod = pjp.getTarget().getClass().getMethod(methodSignature.getName(), methodSignature.getParameterTypes());
    34. } catch (NoSuchMethodException e) {
    35. throw new BusinessException(e);
    36. }
    37. EnableAopLog enableAopLog = null;
    38. if (currentMethod.getDeclaringClass().isAnnotationPresent(EnableAopLog.class)) {
    39. enableAopLog = currentMethod.getDeclaringClass().getAnnotation(EnableAopLog.class);
    40. }
    41. if (currentMethod.isAnnotationPresent(EnableAopLog.class)) {
    42. enableAopLog = currentMethod.getAnnotation(EnableAopLog.class);
    43. }
    44. Object result = null;
    45. try {
    46. result = pjp.proceed();
    47. if (null == enableAopLog) {
    48. return result;
    49. }
    50. if (enableAopLog.printResponse()) {
    51. outInfo.put("response", result);
    52. }
    53. } catch (Throwable e) {
    54. outInfo.put("timeout", TimeRangeStrUtil.parseTime(System.currentTimeMillis() - start));
    55. throw e;
    56. }
    57. if (enableAopLog.printRequest()) {
    58. outInfo.put("fun", currentMethod.getName());
    59. outInfo.put("parameters", pjp.getArgs());
    60. }
    61. if (enableAopLog.printResponse()) {
    62. outInfo.put("timeout", TimeRangeStrUtil.parseTime(System.currentTimeMillis() - start));
    63. }
    64. LogLevel logLevel = enableAopLog.logLevel();
    65. switch (logLevel) {
    66. case OFF:
    67. break;
    68. case DEBUG:
    69. log.debug(LOG_PRE + " 请求:" + JSON.toJSONString(outInfo));
    70. break;
    71. case TRACE:
    72. log.trace(LOG_PRE + " 请求:" + JSON.toJSONString(outInfo));
    73. break;
    74. case INFO:
    75. log.info(LOG_PRE + " 请求:" + JSON.toJSONString(outInfo));
    76. break;
    77. case WARN:
    78. log.warn(LOG_PRE + " 请求:" + JSON.toJSONString(outInfo));
    79. break;
    80. case ERROR:
    81. log.error(LOG_PRE + " 请求:" + JSON.toJSONString(outInfo));
    82. break;
    83. case FATAL:
    84. log.error(LOG_PRE + " 请求:" + JSON.toJSONString(outInfo));
    85. break;
    86. default:
    87. break;
    88. }
    89. return result;
    90. }
    91. }