Search in sources :

Example 1 with Log

use of com.diboot.iam.annotation.Log in project diboot by dibo-software.

the class LogAspect method buildOperationLog.

/**
 * 构建操作日志对象
 * @param joinPoint
 * @return
 */
private IamOperationLog buildOperationLog(JoinPoint joinPoint) {
    IamOperationLog operationLog = new IamOperationLog();
    // 当前请求信息
    RequestAttributes ra = RequestContextHolder.getRequestAttributes();
    HttpServletRequest request = ((ServletRequestAttributes) ra).getRequest();
    operationLog.setRequestMethod(request.getMethod()).setRequestUri(request.getRequestURI()).setRequestIp(HttpHelper.getRequestIp(request));
    // 记录url请求参数 及 body参数
    Map<String, Object> paramsMap = new HashMap<>();
    if (V.notEmpty(request.getQueryString())) {
        paramsMap.put("query", request.getQueryString());
    }
    // 补充注解信息
    MethodSignature signature = (MethodSignature) joinPoint.getSignature();
    Method method = signature.getMethod();
    Log logAnno = AnnotationUtils.getAnnotation(method, Log.class);
    // 保存requestBody数据
    if (logAnno.saveRequestData()) {
        Object[] bodyParams = joinPoint.getArgs();
        if (V.notEmpty(bodyParams)) {
            for (Object arg : bodyParams) {
                if (arg != null && isSimpleDataType(arg)) {
                    paramsMap.put(arg.getClass().getSimpleName(), arg);
                }
            }
        }
    }
    String paramsJson = null;
    if (V.notEmpty(paramsMap)) {
        paramsJson = JSON.stringify(paramsMap);
    }
    operationLog.setRequestParams(paramsJson);
    String businessObj = logAnno.businessObj();
    if (V.isEmpty(businessObj)) {
        Class clazz = method.getDeclaringClass();
        Class entityClazz = BeanUtils.getGenericityClass(clazz, 0);
        if (entityClazz != null) {
            businessObj = entityClazz.getSimpleName();
        } else {
            log.warn("@Log(operation='{}') 注解未识别到class泛型参数,请指定 businessObj", logAnno.operation());
        }
    }
    String appModule = null;
    // 自动识别appModule
    operationLog.setAppModule(appModule).setBusinessObj(businessObj).setOperation(logAnno.operation());
    return operationLog;
}
Also used : MethodSignature(org.aspectj.lang.reflect.MethodSignature) HashMap(java.util.HashMap) Log(com.diboot.iam.annotation.Log) IamOperationLog(com.diboot.iam.entity.IamOperationLog) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) IamOperationLog(com.diboot.iam.entity.IamOperationLog) RequestAttributes(org.springframework.web.context.request.RequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) Method(java.lang.reflect.Method) HttpServletRequest(javax.servlet.http.HttpServletRequest)

Aggregations

Log (com.diboot.iam.annotation.Log)1 IamOperationLog (com.diboot.iam.entity.IamOperationLog)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 MethodSignature (org.aspectj.lang.reflect.MethodSignature)1 RequestAttributes (org.springframework.web.context.request.RequestAttributes)1 ServletRequestAttributes (org.springframework.web.context.request.ServletRequestAttributes)1