Search in sources :

Example 1 with OperLogEntity

use of com.hb0730.boot.admin.project.monitor.operation.model.entity.OperLogEntity in project boot-admin by hb0730.

the class LogAspectj method handleLog.

/**
 * 日志处理
 */
void handleLog(final JoinPoint joinPoint, Object jsonResult, final Exception e) {
    try {
        Log log = getAnnotationLog(joinPoint);
        if (check(joinPoint, log)) {
            return;
        }
        // 当前用户
        OperLogEntity entity = new OperLogEntity();
        // 状态
        entity.setStatus(StatusEnum.SUCCESS.getValue());
        // 操作类型
        entity.setOperType(log.businessType().getValue());
        // 请求用户
        User currentUser = SecurityUtils.getCurrentUser();
        if (null != currentUser) {
            entity.setUsername(currentUser.getUsername());
            entity.setCreateUserId(currentUser.getId());
            entity.setCreateTime(LocalDateTimeUtils.now());
        }
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        if (null == attributes) {
            return;
        }
        // 请求ip
        String ip = "";
        // 请求地址
        String requestUrl = "";
        // 请求方式
        String requestMethod = "";
        if (attributes instanceof ServletRequestAttributes) {
            HttpServletRequest request = ((ServletRequestAttributes) attributes).getRequest();
            ip = ServletUtil.getClientIP(request);
            // 请求地址
            requestUrl = request.getRequestURI();
            requestMethod = request.getMethod();
        }
        entity.setOperIp(ip);
        // 描述
        // 类描述
        String controllerDescription = "";
        ClassDescribe classDescribe = joinPoint.getTarget().getClass().getAnnotation(ClassDescribe.class);
        if (null != classDescribe) {
            controllerDescription = classDescribe.value();
        }
        // 方法描述
        String controllerMethodDescription = getDescribe(log);
        if (log.controllerApiValue()) {
            entity.setDescription(StringUtils.join(controllerDescription, controllerMethodDescription));
        } else {
            entity.setDescription(controllerMethodDescription);
        }
        // 请求地址
        entity.setRequestUrl(requestUrl);
        // 请求方式
        entity.setRequestMethod(requestMethod);
        // 操作方法
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        entity.setOperMethod(className + "." + methodName);
        // 请求参数
        if (log.request()) {
            HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
            String requestParamsValue = getRequestValue(joinPoint, request, log.paramsName());
            entity.setRequestParams(requestParamsValue);
        }
        // 返回参数
        if (log.response()) {
            String result = JsonUtils.objectToJson(null == jsonResult ? "" : jsonResult);
            entity.setRequestResult(result);
        }
        if (null != e) {
            if (log.requestByError()) {
                String result = JsonUtils.objectToJson(null == jsonResult ? "" : jsonResult);
                entity.setRequestResult(result);
                String message = ExceptionUtils.getExceptionMessage(e);
                entity.setErrorMessage(StringUtils.substring(message, 0, 2000));
                entity.setStatus(StatusEnum.FAIL.getValue());
            }
        }
        AsyncManager.me().execute(AsyncFactory.recordOperLog(entity));
    } catch (Exception e1) {
        e1.printStackTrace();
        if (null != e) {
            e.printStackTrace();
        }
        // 记录本地异常日志
        LOGGER.error("==前置通知异常==");
        LOGGER.error("异常信息:{}", e1.getMessage());
        if (null != e) {
            throw new BusinessException(e.getMessage());
        } else {
            throw new BusinessException(e1.getMessage());
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) BusinessException(com.hb0730.boot.admin.exceptions.BusinessException) User(com.hb0730.boot.admin.security.model.User) Log(com.hb0730.boot.admin.annotation.Log) OperLogEntity(com.hb0730.boot.admin.project.monitor.operation.model.entity.OperLogEntity) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) ClassDescribe(com.hb0730.boot.admin.annotation.ClassDescribe) RequestAttributes(org.springframework.web.context.request.RequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) BusinessException(com.hb0730.boot.admin.exceptions.BusinessException)

Aggregations

ClassDescribe (com.hb0730.boot.admin.annotation.ClassDescribe)1 Log (com.hb0730.boot.admin.annotation.Log)1 BusinessException (com.hb0730.boot.admin.exceptions.BusinessException)1 OperLogEntity (com.hb0730.boot.admin.project.monitor.operation.model.entity.OperLogEntity)1 User (com.hb0730.boot.admin.security.model.User)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 RequestAttributes (org.springframework.web.context.request.RequestAttributes)1 ServletRequestAttributes (org.springframework.web.context.request.ServletRequestAttributes)1