自定义AOP监控controller层的方法执行信息打印日志

自定义AOP监控controller层的方法执行

WebLog

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@Documented
public @interface WebLog {
    String value() default "";

    String module() default "";

    String desc() default "";

    String type() default "";
}

AopConfig

@Slf4j
@Aspect
@Component
public class AopConfig {

@Pointcut("@annotation(com.dem.annotation.WebLog)")
public void webLog(){

}
@Around("webLog()")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
    //拿取WebLog里面的值(module:模块,desc:描述)
    MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
    Method method = methodSignature.getMethod();
    WebLog webLogger = method.getAnnotation(WebLog.class);
    String module = webLogger.module();
    String desc = webLogger.desc();

    long startTime = System.currentTimeMillis();
    Object result = null;
    try {
        result = joinPoint.proceed();
        log.info("Response:" + JacksonUtils.beanToJson(result));
        log.info("耗时:" + (System.currentTimeMillis() - startTime) + "ms");
        if (StringUtils.checkIsStrNull(StringUtils.obj2str(result))){
            throw new CommonException(ErrorCode.LOG_CODE_0002);
        }
        JSONObject obj = (JSONObject) JSONObject.toJSON(result);
        String status = obj.getString("code");
        String msg = obj.getString("error");

    } catch (Throwable e) {
        JSONObject obj = (JSONObject) JSONObject.toJSON(e);
        String status = obj.getString("code");
        String msg = obj.getString("msg");

        log.error("错误码:"+status);
        log.error("错误日志:"+e.getMessage());
        log.error(e.getMessage(),e);
        throw new CommonException(ErrorCode.FAIL);
    }
    return result;
}

@Before(value="webLog()")
    public void beforeControll(JoinPoint joinPoint) throws Exception{
    ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    HttpServletRequest request = attributes.getRequest();
    log.info("==================Start=================");
    log.info("URL:" + request.getRequestURL().toString());
    log.info("QueryString:" + request.getQueryString());
    log.info("Description:" + getLogValue(joinPoint));
    log.info("Method:" + request.getMethod());
    log.info("Class Method:" + joinPoint.getSignature().getDeclaringTypeName() + "," + joinPoint.getSignature().getName());
    log.info("客户端IP:" + request.getRemoteAddr());
    log.info("请求参数:" + JacksonUtils.beanToJson(joinPoint.getArgs()));
}
    @After(value="webLog()")
    public void afterControll() throws Exception{
    log.info("结束了");
    }
    @AfterReturning("webLog()")
    public void doAfter() throws Exception {
        log.info("==================End=================");
    }

    private String getLogValue(JoinPoint joinPoint) {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        WebLog webLogger = method.getAnnotation(WebLog.class);
        return webLogger.value();
    }
}

然后在controller层方法上方加注解
@WebLog(module = “模块名称”,desc =“描述” )

其中用到了几个工具类,需要请在博客工具文章中寻找

你可能感兴趣的:(JAVA)