美文网首页
spring boot用aop做统一请求日志处理

spring boot用aop做统一请求日志处理

作者: dancer4code | 来源:发表于2019-10-09 23:10 被阅读0次

    1.定义切面、切点@Aspect+@Pointcut

    package com.d4c.exception.demo.aspect;
    
    import lombok.extern.slf4j.Slf4j;
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.annotation.*;
    import org.springframework.stereotype.Component;
    import org.springframework.web.context.request.RequestContextHolder;
    import org.springframework.web.context.request.ServletRequestAttributes;
    
    import javax.servlet.http.HttpServletRequest;
    
    /**
     * Created with IntelliJ IDEA.
     * User: liangqing.zhao(zlq)
     * Date: 2019/10/9 20:54
     * Description:
     */
    @Aspect
    @Component
    @Slf4j
    public class HttpAspect {
        @Pointcut("execution(public * com.d4c.exception.demo.controller.*.*(..))")
        public void pointCut(){}
    
        @Before("pointCut()")
        public void doBefore(JoinPoint joinPoint){
            ServletRequestAttributes attributes= (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            log.info("url={}",request.getRequestURL());
            //method
            log.info("method={}", request.getMethod());
    
            //ip
            log.info("ip={}", request.getRemoteAddr());
    
            //类方法
            log.info("method={}", joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
    
            //参数
            log.info("args={}", joinPoint.getArgs());
    
        }
    
        @After("pointCut()")
        public void doAfter(){
            log.info("-----after------");
    
        }
        @AfterReturning(returning = "object",pointcut = "pointCut()")
        public void doAfterReturning(Object object){
            log.info("return={}",object);
    
        }
    }
    

    2.简单接口

    package com.d4c.exception.demo.controller;
    
    
    import com.d4c.exception.demo.pojo.ResultInfo;
    import com.d4c.exception.demo.pojo.Student;
    import com.d4c.exception.demo.service.StudentService;
    import com.d4c.exception.demo.utils.ResultInfoOldUtil;
    import com.d4c.exception.demo.utils.ResultInfoUtil;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.util.StringUtils;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.List;
    
    
    /**
     * Created with IntelliJ IDEA.
     * User: liangqing.zhao(zlq)
     * Date: 2019/10/4 18:16
     * Description:
     */
    @RestController
    @RequestMapping("student")
    public class StudentController {
        @Autowired
        private StudentService studentService;
    
    
        @RequestMapping("add")
        public ResultInfo add() {
            Student stu = new Student(null, "黄麒英", 48);
            studentService.insert(stu);
            return ResultInfoUtil.success();
        }
    
    
        @RequestMapping("getOne")
        public ResultInfo getOne(@RequestParam(required = false) String id) {
            Student student = studentService.get(id);
            return ResultInfoUtil.success(student);
        }
    
        @RequestMapping("findByIdFirst")
        public Student findByIdFirst(@RequestParam String id) {
            return studentService.findById(id).orElse(null);
        }
    
        @RequestMapping("findAllFirst")
        public List<Student> findByIdFirst() {
            return studentService.list();
        }
    
        @RequestMapping("findByIdSecond")
        public ResultInfo findByIdSecond(@RequestParam String id) {
            Student stu = studentService.findById(id).orElse(null);
            ResultInfo resultInfo = new ResultInfo();
            resultInfo.setCode(1);
            resultInfo.setMsg("成功!");
            resultInfo.setData(stu);
            return resultInfo;
        }
    
        @RequestMapping("findAllSecond")
        public ResultInfo findByIdSecond() {
            List<Student> listStudent = studentService.list();
            ResultInfo resultInfo = new ResultInfo();
            resultInfo.setCode(1);
            resultInfo.setMsg("成功!");
            resultInfo.setData(listStudent);
            return resultInfo;
        }
    
        @RequestMapping("findByIdThird")
        public ResultInfo findByIdThird(@RequestParam String id) {
            Student stu = studentService.findById(id).orElse(null);
            return ResultInfoOldUtil.success(stu);
        }
    
        @RequestMapping("findAllThird")
        public ResultInfo findByIdThird() {
            List<Student> listStudent = studentService.list();
            return ResultInfoOldUtil.success(listStudent);
        }
    
        @RequestMapping("findByIdFourth")
        public ResultInfo findByIdFourth(@RequestParam String id) {
            try {
                Student stu = studentService.findById(id).orElse(null);
                Integer count = 1 / 0;
                return ResultInfoOldUtil.success(stu);
            } catch (Exception e) {
                e.printStackTrace();
                return ResultInfoOldUtil.error();
            }
        }
    
        @RequestMapping("findAllFourth")
        public ResultInfo findByIdFourth() throws Exception {
            try {
                List<Student> listStudent = studentService.list();
                throw new Exception("出现异常");
            } catch (Exception e) {
                e.printStackTrace();
                return ResultInfoOldUtil.error();
            }
        }
    
        @RequestMapping("findByIdFifth")
        public ResultInfo findByIdFifth(@RequestParam String id) {
            Student stu = studentService.findById(id).orElse(null);
            Integer count = 1 / 0;
            return ResultInfoOldUtil.success(stu);
        }
    
        @RequestMapping("findAllFifth")
        public ResultInfo findByIdFifth() throws Exception {
            List<Student> listStudent = studentService.list();
            throw new Exception("出现异常");
        }
    
        @RequestMapping("findById")
        public ResultInfo findById(@RequestParam(required = false) String id) {
            Student byId = studentService.findById(id).orElse(null);
            return ResultInfoUtil.success(byId);
    
    
        }
    
        @RequestMapping("findAll")
        public ResultInfo findAll() {
            List<Student> list = studentService.list();
            return ResultInfoUtil.success(list);
        }
    }
    

    3.请求接口测试

    请求地址

    4.结果

    2019-10-09 23:02:09 [http-nio-8000-exec-4] INFO  com.d4c.exception.demo.aspect.HttpAspect -url=http://localhost:8000/student/findAll
    2019-10-09 23:02:09 [http-nio-8000-exec-4] INFO  com.d4c.exception.demo.aspect.HttpAspect -method=GET
    2019-10-09 23:02:09 [http-nio-8000-exec-4] INFO  com.d4c.exception.demo.aspect.HttpAspect -ip=0:0:0:0:0:0:0:1
    2019-10-09 23:02:09 [http-nio-8000-exec-4] INFO  com.d4c.exception.demo.aspect.HttpAspect -method=com.d4c.exception.demo.controller.StudentController.findAll
    2019-10-09 23:02:09 [http-nio-8000-exec-4] INFO  com.d4c.exception.demo.aspect.HttpAspect -args={}
    Hibernate: select student0_.id as id1_0_, student0_.age as age2_0_, student0_.name as name3_0_ from student student0_
    2019-10-09 23:02:09 [http-nio-8000-exec-4] INFO  com.d4c.exception.demo.aspect.HttpAspect ------after------
    2019-10-09 23:02:09 [http-nio-8000-exec-4] INFO  com.d4c.exception.demo.aspect.HttpAspect -return=ResultInfo(code=1, msg=success, data=[Student(id=d4c-04b6feca-a37c-4942-9042-d9da28c167fe, na
    

    每个方法都拦截了,并打印出信息。搞定了

    想自定义日志信息可以参考logback.xml

    <?xml version="1.0" encoding="utf-8" ?>
    <!-- 从高到地低 OFF 、 FATAL 、 ERROR 、 WARN 、 INFO 、 DEBUG 、 TRACE 、 ALL -->
    <!-- 日志输出规则  根据当前ROOT 级别,日志输出时,级别高于root默认的级别时  会输出 -->
    <!-- 以下  每个配置的 filter 是过滤掉输出文件里面,会出现高级别文件,依然出现低级别的日志信息,通过filter 过滤只记录本级别的日志-->
    
    <!-- 属性描述 scan:性设置为true时,配置文件如果发生改变,将会被重新加载,默认值为true scanPeriod:设置监测配置文件是否有修改的时间间隔,如果没有给出时间单位,
    默认单位是毫秒。当scan为true时,此属性生效。默认的时间间隔为1分钟。debug:当此属性设置为true时,将打印出logback内部日志信息,实时查看logback运行状态。默认值为false。 -->
    <configuration scan="false" scanPeriod="60 seconds" debug="false">
        <!-- 定义日志文件 输入位置 -->
        <property name="logPath" value="d:/test_log" />
        <!-- 日志最大的历史 30天 -->
        <property name="maxHistory" value="30"/>
    
        <!-- 配置项, 通过此节点配置日志输出位置(控制台、文件、数据库)、输出格式等-->
        <!-- ConsoleAppender代表输出到控制台 -->
        <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
            <!-- layout代表输出格式 -->
            <layout class="ch.qos.logback.classic.PatternLayout">
                <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %highlight(%-5level) %cyan(%logger) -%.-100msg%n</pattern>
            </layout>
        </appender>
        <!-- 日志输出文件 -->
        <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <encoder>
                <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger -%msg%n</pattern>
            </encoder>
            <!-- 滚动记录文件,先将日志记录到指定文件,当符合某个条件时,将日志记录到其他文件 RollingFileAppender-->
            <!-- 滚动策略,它根据时间来制定滚动策略.既负责滚动也负责触发滚动 -->
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <!-- 输出路径 -->
                <fileNamePattern>${logPath}/info/%d.log</fileNamePattern>
                <!-- 可选节点,控制保留的归档文件的最大数量,超出数量就删除旧文件假设设置每个月滚动,且<maxHistory>是6,
                则只保存最近6个月的文件,删除之前的旧文件。注意,删除旧文件是,那些为了归档而创建的目录也会被删除-->
                <maxHistory>${maxHistory}</maxHistory>
            </rollingPolicy>
            <!-- 按照固定窗口模式生成日志文件,当文件大于20MB时,生成新的日志文件。窗口大小是1到3,当保存了3个归档文件后,将覆盖最早的日志。
            <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
              <fileNamePattern>${logPath}/%d{yyyy-MM-dd}/.log.zip</fileNamePattern>
              <minIndex>1</minIndex>
              <maxIndex>3</maxIndex>
            </rollingPolicy>   -->
            <!-- 查看当前活动文件的大小,如果超过指定大小会告知RollingFileAppender 触发当前活动文件滚动
            <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
                <maxFileSize>5MB</maxFileSize>
            </triggeringPolicy>   -->
        </appender>
        <!-- 特殊记录Error日志 -->
        <appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <!-- 只记录ERROR级别日志,添加范围过滤,可以将该类型的日志特殊记录到某个位置 -->
            <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
                <level>ERROR</level>
            </filter>
            <encoder>
                <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger -%msg%n</pattern>
            </encoder>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <fileNamePattern>${logPath}/error/%d.log</fileNamePattern>
                <!-- 日志最大的历史 60天 -->
                <maxHistory>60</maxHistory>
            </rollingPolicy>
        </appender>
        <appender name="fileWarnLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <!-- 只记录ERROR级别日志,添加范围过滤,可以将该类型的日志特殊记录到某个位置 -->
            <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
                <level>WARN</level>
            </filter>
            <encoder>
                <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger -%msg%n</pattern>
            </encoder>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <fileNamePattern>${logPath}/warn/%d.log</fileNamePattern>
                <!-- 日志最大的历史 60天 -->
                <maxHistory>60</maxHistory>
            </rollingPolicy>
        </appender>
    
        <!-- 根节点,表名基本的日志级别,里面可以由多个appender规则 -->
        <!-- level="ALL"代表基础日志级别为ALL -->
        <root level="info">
            <!-- 引入控制台输出规则 -->
            <appender-ref ref="consoleLog" />
            <appender-ref ref="fileInfoLog" />
            <appender-ref ref="fileErrorLog" />
            <appender-ref ref="fileWarnLog" />
        </root>
    </configuration>
    

    更多请参考logging日志-log4j、log4j 2、logback、commons-logging和.....

    上述代码请参考gitee源码地址

    相关文章

      网友评论

          本文标题:spring boot用aop做统一请求日志处理

          本文链接:https://www.haomeiwen.com/subject/nkmnpctx.html