• Spring 项目全局日志最佳实践


    需求

    记录前端的每一个请求的信息,大概就是下面这个样子,以后出现问题的时候,便于定位问题

    请求方式:GET
    请求地址:http://localhost:8001/memo/taskLog/list
    请求参数:{“taskId”:“47”}
    请求耗时:17ms

    技术选型

    1. Interceptor (无法实现)

    最开始想到是拦截器:实现 HandlerInterceptor
    1. 可以配置拦截规则
    2. 请求进来和响应出去都有对应的方法 preHandleafterCompletion
    3. 能拿到请求参数,尽管处理请求逻辑有点麻烦
    	String reqParam = "";
    	if (Method.GET.toString().equalsIgnoreCase(method)) {
    		JSONObject jsonObject = new JSONObject();
    		Enumeration<String> parameterNames = request.getParameterNames();
    		if (parameterNames.hasMoreElements()) {
    			String paramName = parameterNames.nextElement();
    			String param = request.getParameter(paramName);
    			jsonObject.put(paramName, param);
    		}
    		reqParam = jsonObject.toString();
    	} else {
    		// 非 GET 方式都按照 POST 处理,且只处理 json 格式,因为导入时文件记录不了
    		if (StrUtil.isNotBlank(request.getContentType()) && request.getContentType().startsWith(MimeTypeUtils.APPLICATION_JSON_VALUE)) {
    			StringBuilder stringBuilder = new StringBuilder();
    			String str;
    			BufferedReader reader = request.getReader();
    			while ((str = reader.readLine()) != null) {
    				stringBuilder.append(str);
    			}
    			reqParam = JSON.parse(stringBuilder.toString()).toString();
    		}
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    实际执行一开始就报错了

    因为在 HttpServletRequest 获取请求体body参数的时候,是通过流的方式获取,就会多读取一次请求参数,而参数只允许读取一次,所以报错了
    解决方案: 重写HttpServletRequest 为可重复读取的request,写好之后又遇到问题,拦截器修改不了request,所以只能改用过滤器

    2.Filter (虽然能实现,但是处理比较麻烦)

    过滤器:实现Filter
    doFilter方法重写HttpServletRequest 为可重复读取的request,问题解决
    正常场景记录日志没有问题,但是抛异常捕获不到,需要配合 @RestControllerAdvice@ExceptionHandler 在抛异常场景特殊处理,还要配合 ThreadLocal 传递异常信息

    3.Aop (最终解决方案)(参数获取简单、异常获取方便)

    直接配置所有controller 的方法作为切面
    通过环绕通知around,能轻松获取请求参数,也不用重写HttpServletRequest,也不用写一堆处理逻辑,异常也能直接捕获
    请求方式和请求地址信息,通过 HttpServletRequest 获取

    package com.gly.memo.common.aspect;
    
    import cn.hutool.core.util.StrUtil;
    import cn.hutool.extra.servlet.ServletUtil;
    import com.alibaba.fastjson2.JSON;
    import com.gly.memo.common.exception.MyException;
    import com.gly.memo.dto.vo.UserInfoVo;
    import com.gly.memo.entity.SysLog;
    import com.gly.memo.service.SysLogService;
    import com.gly.memo.utils.AspectUtils;
    import com.gly.memo.utils.ThreadLocalUtils;
    import io.swagger.annotations.ApiOperation;
    import lombok.extern.slf4j.Slf4j;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.springframework.stereotype.Component;
    import org.springframework.util.MimeTypeUtils;
    
    import javax.annotation.Resource;
    import javax.servlet.http.HttpServletRequest;
    import java.net.InetAddress;
    import java.time.Duration;
    import java.time.LocalDateTime;
    
    /**
     * 日志切面
     *
     * @author gaojie
     * @date 2022-11-11
     */
    @Slf4j
    @Aspect
    @Component
    public class LogAspect {
    
        @Resource
        private HttpServletRequest request;
        @Resource
        private SysLogService sysLogService;
    
        /**
         * 使用环绕通知,拦截所有的 controller 请求
         */
        @Around(value = "within(com.gly.memo.controller.*)")
        public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
            UserInfoVo currentUser = ThreadLocalUtils.getCurrentUser();
            SysLog sysLog = new SysLog();
    
            // 1.日志基本参数
            sysLog.setServerIp(InetAddress.getLocalHost().getHostAddress());
            sysLog.setClientIp(ServletUtil.getClientIP(request));
            sysLog.setUri(request.getRequestURI());
            sysLog.setMethod(request.getMethod());
            sysLog.setOperation(getOperation(joinPoint));
            sysLog.setReqJson(getReqJson(joinPoint));
            sysLog.setStartTime(LocalDateTime.now());
            sysLog.setCreateBy(currentUser.getId());
            sysLog.setCreateName(currentUser.getName());
    
            try {
                // 2.执行业务逻辑
                Object result = joinPoint.proceed(joinPoint.getArgs());
    
                // 3.1.请求成功后操作
                sysLog.setSuccess(true);
                sysLog.setResJson(JSON.toJSONString(result));
                return result;
            } catch (Exception e) {
                log.info("异常信息: {}", e.getMessage());
    
                // 3.2.抛异常后操作
                sysLog.setSuccess(false);
                sysLog.setException(e.getMessage());
                sysLog.setBusinessException(e instanceof MyException);
                throw e;
            } finally {
                // 3.3.任务计时
                sysLog.setEndTime(LocalDateTime.now());
                sysLog.setDuration((int) Duration.between(sysLog.getStartTime(), sysLog.getEndTime()).toMillis());
    
                // 4.1.log方式打印
                log.info("请求操作:{}", sysLog.getOperation());
                log.info("请求方式:{}", sysLog.getMethod());
                log.info("请求地址:{}", request.getRequestURL());
                log.info("请求参数:{}", sysLog.getReqJson());
                log.info("请求耗时:{}ms", sysLog.getDuration());
    
                // 4.2.持久化
                sysLogService.save(sysLog);
            }
        }
    
        /**
         * 获取请求参数 JSON
         * 1.contentType 为空,表示为 GET 请求
         * 2.contentType 前缀为 application/json,表示非 GET 请求,但是请求参数为 JSON 格式
         * 3.否则有可能为导入的文件,请求过大而且格式为不可读,记录到日志无意义,所以不记录
         */
        private String getReqJson(ProceedingJoinPoint joinPoint) {
            String contentType = request.getContentType();
            if (StrUtil.isBlank(contentType) || contentType.startsWith(MimeTypeUtils.APPLICATION_JSON_VALUE)) {
                return JSON.toJSONString(joinPoint.getArgs());
            } else {
                return "非普通请求参数,暂不记录";
            }
        }
    
        /**
         * 获取操作类型
         */
        private String getOperation(ProceedingJoinPoint joinPoint) {
            ApiOperation annotation = AspectUtils.getAnnotation(joinPoint, ApiOperation.class);
            return annotation.value();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117

    缺点就是过滤器和拦截器处理的一段时间没有记录,不是特别精确
    (不过核心功能已经实现,处理时间偏差几十ms可以接受,如果想要时间最精确,实现ServletRequestListener记录直接最准确,不过增加了系统复杂性)

  • 相关阅读:
    Qt使用FFmpeg的静态库
    【docker】安装clickhouse
    2022-01-28-JQuery
    Python 08学习之文件操作
    VScode 关闭鼠标悬停提示
    Linux命令-sed
    总结下.NET后端已经熟悉或者使用过了这么多东西,有没你喜欢用的
    树递归遍历和Mirrors遍历
    2022年最新Python大数据之Python基础【九】面向对象与继承
    Hive学习笔记:05Hive中常用分析函数使用解析
  • 原文地址:https://blog.csdn.net/gaojie_csdn/article/details/127810618