menu 李昊天的个人博客
全局Controller切面日志
307 浏览 | 2021-04-05 | 分类:SpringBoot | 标签:AOP,项目经验,代码片段

切面日志类

  • JSONUtil 是hutools的工具类中的一个工具,可以自行写toString方法。
  • BaseResult 是自定义的结果类,放在最后

@Aspect
@Component// 非常重要
@Log4j2
public class AllControllerAOP {
    /**
     * ..表示包及子包 该方法代表controller层的所有方法  TODO 路径需要根据自己项目定义
     */
    @Pointcut("execution(public * com.hanhaihrms.serve.*.controller..*.*(..))")
    public void controllerMethod() {
    }


    /**
     * 方法执行前
     *
     * @param joinPoint
     * @throws Exception
     */
    @Before("controllerMethod()")
    public void LogRequestInfo(JoinPoint joinPoint) {

        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        StringBuilder requestLog = new StringBuilder();
        Signature signature = joinPoint.getSignature();
        // 获取swager的注解
        ApiOperation annotation = ((MethodSignature) signature)
                .getMethod()
                .getAnnotation(ApiOperation.class);
        if (annotation!=null){
            requestLog.append(annotation.value()).append("\t");
        }


        requestLog.append("请求信息:").append("URL = {").append(request.getRequestURI()).append("},\t")
                .append("请 求方式 = {").append(request.getMethod()).append("},\t")
                .append("请求IP = {").append(request.getRemoteAddr()).append("},\t")
                .append("类方法 = {").append(signature.getDeclaringTypeName()).append(".")
                .append(signature.getName()).append("},\t");

        // 处理请求参数
        String[] paramNames = ((MethodSignature) signature).getParameterNames();
        Object[] paramValues = joinPoint.getArgs();
        int paramLength = null == paramNames ? 0 : paramNames.length;
        if (paramLength == 0) {
            requestLog.append("请求参数 = {} ");
        } else {
            requestLog.append("请求参数 = [");
            for (int i = 0; i < paramLength - 1; i++) {
                requestLog.append(paramNames[i]).append("=").append(JSONUtil.toJsonStr(paramValues[i])).append(",");
            }
            requestLog.append(paramNames[paramLength - 1]).append("=").append(JSONUtil.toJsonStr(paramValues[paramLength - 1])).append("]");
        }

        log.info(requestLog.toString());
    }



    @AfterReturning(returning = "baseResult", pointcut =
            "controllerMethod()")
    public void logResultVOInfo(BaseResult baseResult) throws Exception {
        log.info("请求结果:" + baseResult.getCode() + "\t" + baseResult.getMsg()+"\t"+ JSONUtil.toJsonStr(baseResult.getData()));
    }

}

BaseResult

@lombok.AllArgsConstructor
@Data
public class BaseResult<T> implements Serializable {
    private Integer code;
    private String msg;
    private T data;

    public BaseResult(Code code){
        this.code=code.getCode();
        this.msg=code.getMsg();
    }
    public BaseResult(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }


    public Integer getCode() {
        return code;
    }

    public BaseResult<T> setCode(Integer code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public BaseResult<T>  setMsg(String msg) {
        this.msg = msg;

        return this;
    }

    public T getData() {
        return data;
    }

    public BaseResult<T>  setData(T data) {
        this.data = data;

        return this;
    }
    public String toString(){
        return  "{" +
                "\"code\"=" + code +","+
                "\"msg\"=" + "\""+msg+"\"" +
                '}';
    }
}
知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议

发表评论

email
web

全部评论 (暂无评论)

info 还没有任何评论,你来说两句呐!