Search in sources :

Example 1 with LogBody

use of com.nb6868.onex.common.log.LogBody in project onex-boot by zhangchaoxu.

the class BaseExceptionHandler method saveLog.

/**
 * 保存异常日志
 */
protected void saveLog(HttpServletRequest request, Exception ex) {
    LogBody logEntity = new LogBody();
    logEntity.setStoreType("db");
    logEntity.setType("error");
    logEntity.setState(0);
    // 请求相关信息
    if (request == null) {
        request = HttpContextUtils.getHttpServletRequest();
    }
    if (null != request) {
        logEntity.setUri(request.getRequestURI());
        // 记录内容
        Dict requestParams = Dict.create();
        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 (HttpMethod.POST.name().equalsIgnoreCase(request.getMethod())) {
            try {
                requestParams.set("params", IoUtil.read(request.getInputStream()).toString());
            } catch (IOException e) {
                log.error("读取流失败", e);
            }
        }
        logEntity.setRequestParams(requestParams);
    }
    // 异常信息
    logEntity.setContent(ExceptionUtil.stacktraceToString(ex));
    // 保存
    try {
        logService.saveLog(logEntity);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Dict(cn.hutool.core.lang.Dict) LogBody(com.nb6868.onex.common.log.LogBody) IOException(java.io.IOException) UnauthenticatedException(org.apache.shiro.authz.UnauthenticatedException) NoHandlerFoundException(org.springframework.web.servlet.NoHandlerFoundException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) MissingServletRequestParameterException(org.springframework.web.bind.MissingServletRequestParameterException) IOException(java.io.IOException) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) MaxUploadSizeExceededException(org.springframework.web.multipart.MaxUploadSizeExceededException) MethodArgumentTypeMismatchException(org.springframework.web.method.annotation.MethodArgumentTypeMismatchException) DuplicateKeyException(org.springframework.dao.DuplicateKeyException) UnauthorizedException(org.apache.shiro.authz.UnauthorizedException) ConstraintViolationException(javax.validation.ConstraintViolationException) HttpRequestMethodNotSupportedException(org.springframework.web.HttpRequestMethodNotSupportedException)

Example 2 with LogBody

use of com.nb6868.onex.common.log.LogBody 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)2 LogBody (com.nb6868.onex.common.log.LogBody)2 LogOperation (com.nb6868.onex.common.annotation.LogOperation)1 OnexException (com.nb6868.onex.common.exception.OnexException)1 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 ConstraintViolationException (javax.validation.ConstraintViolationException)1 UnauthenticatedException (org.apache.shiro.authz.UnauthenticatedException)1 UnauthorizedException (org.apache.shiro.authz.UnauthorizedException)1 MethodSignature (org.aspectj.lang.reflect.MethodSignature)1 DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)1 DuplicateKeyException (org.springframework.dao.DuplicateKeyException)1 HttpMethod (org.springframework.http.HttpMethod)1 HttpMessageNotReadableException (org.springframework.http.converter.HttpMessageNotReadableException)1 HttpRequestMethodNotSupportedException (org.springframework.web.HttpRequestMethodNotSupportedException)1 MethodArgumentNotValidException (org.springframework.web.bind.MethodArgumentNotValidException)1 MissingServletRequestParameterException (org.springframework.web.bind.MissingServletRequestParameterException)1 MethodArgumentTypeMismatchException (org.springframework.web.method.annotation.MethodArgumentTypeMismatchException)1 MaxUploadSizeExceededException (org.springframework.web.multipart.MaxUploadSizeExceededException)1