Search in sources :

Example 1 with LogOperation

use of com.nb6868.onex.common.annotation.LogOperation in project onex-boot by zhangchaoxu.

the class LogOperationAspect method saveLog.

/**
 * 保存日志
 */
private void saveLog(ProceedingJoinPoint joinPoint, String params, long time, Integer state, Exception e) {
    MethodSignature signature = (MethodSignature) joinPoint.getSignature();
    LogBody logEntity = new LogBody();
    // 日志记录类型
    String storeType = "db";
    String logType = "operation";
    try {
        Method method = joinPoint.getTarget().getClass().getDeclaredMethod(signature.getName(), signature.getParameterTypes());
        LogOperation annotation = method.getAnnotation(LogOperation.class);
        if (annotation != null) {
            // 注解上的描述
            logEntity.setOperation(annotation.value());
            logType = annotation.type();
            storeType = annotation.storeType();
        }
    } catch (NoSuchMethodException ne) {
        ne.printStackTrace();
    }
    logEntity.setStoreType(storeType);
    logEntity.setState(state);
    logEntity.setRequestTime(time);
    logEntity.setType(logType);
    // 保存错误信息
    if (e != null) {
        logEntity.setContent(e instanceof OnexException ? e.toString() : ExceptionUtil.stacktraceToString(e));
    }
    // 请求参数
    Dict requestParams = Dict.create().set("params", params);
    HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
    if (null != request) {
        logEntity.setUri(request.getRequestURI());
        // 记录内容
        requestParams.set("ip", HttpContextUtils.getIpAddr(request));
        requestParams.set("ua", request.getHeader(HttpHeaders.USER_AGENT));
        requestParams.set("queryString", request.getQueryString());
        requestParams.set("url", request.getRequestURL());
        requestParams.set("method", request.getMethod());
        requestParams.set("contentType", request.getContentType());
    /*if (request instanceof OnexHttpServletRequestWrapper) {
                try {
                    requestParams.set("params", IoUtil.read(request.getInputStream()).toString());
                } catch (IOException ie) {
                    log.error("读取流失败", e);
                }
            }*/
    }
    logEntity.setRequestParams(requestParams);
    logService.saveLog(logEntity);
}
Also used : OnexException(com.nb6868.onex.common.exception.OnexException) HttpServletRequest(javax.servlet.http.HttpServletRequest) LogOperation(com.nb6868.onex.common.annotation.LogOperation) MethodSignature(org.aspectj.lang.reflect.MethodSignature) Dict(cn.hutool.core.lang.Dict) LogBody(com.nb6868.onex.common.log.LogBody) Method(java.lang.reflect.Method) HttpMethod(org.springframework.http.HttpMethod)

Aggregations

Dict (cn.hutool.core.lang.Dict)1 LogOperation (com.nb6868.onex.common.annotation.LogOperation)1 OnexException (com.nb6868.onex.common.exception.OnexException)1 LogBody (com.nb6868.onex.common.log.LogBody)1 Method (java.lang.reflect.Method)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 MethodSignature (org.aspectj.lang.reflect.MethodSignature)1 HttpMethod (org.springframework.http.HttpMethod)1