aop实现controller入参的记录

/ Java / 0 条评论 / 903浏览

aop实现controller入参的记录

有时候我们可能需要对接口的入参进行记录,比如简单的日志记录,这个时候我们就可以使用aop来完成这个功能

1.切面编写

引入aop依赖

<!--aop-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

编写切面

/**
 * @author zuohui
 * @date 2020/7/24 - 16:19
 */
@Aspect
@Component
@Slf4j
public class ReqParamsLogAspect {

//这代表controller包下的所有类的所有方法
    @Pointcut("execution(public * cn.xxx.controller..*.*(..))")
    public void aspectJ() {
    }

  
    @Before("aspectJ()")
    public void doBefore(JoinPoint joinPoint) {
       log.info("*****************请求记录**********************");
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd HH🇲🇲ss");
        ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();//获取request请求
        Map<String, String[]> parameterMap = request.getParameterMap();
        Set<String> keySet = parameterMap.keySet();
        StringBuilder paramInfo = new StringBuilder();

        paramInfo.append("【本次请求入参情况】");
        for (String s : keySet) {
            paramInfo.append("["+s+":"+parameterMap.get(s)[0]+"] ");
        }
        log.info("【请求时间】"+sdf.format(new Date()));
        log.info(paramInfo.toString());
        log.info("【ip】:"+ CommonUtil.getIpAdrress(request));
    }

}

2.希望将error和info的日志分别单独记录
<?xml version="1.0" encoding="UTF-8" ?>

<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <!--<springProperty scope="context" name="springAppName" source="spring.application.name"/>-->

    <!--配置输出到控制台的日志记录器-->
    <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>
                %d - %msg%n
            </pattern>
        </layout>
    </appender>

    <!--配置输出到文件的日志记录器  记录info级别-->
    <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!-- 过滤日志 -->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>DENY</onMatch>  <!-- 如果是error就过滤 -->
            <onMismatch>ACCEPT</onMismatch>  <!-- 如果不是error就不过滤-->
        </filter>

        <encoder>
            <pattern>
                %d - %msg%n
            </pattern>
        </encoder>
        <!--滚动策略-->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--路径-->
            <fileNamePattern>/home/sudo_root/logs/api/info.%d.log</fileNamePattern>
        </rollingPolicy>
    </appender>

    <!--配置输出到文件的日志记录器 记录error级别 -->
    <appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!-- 过滤日志 -->
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>ERROR</level>
        </filter>

        <encoder>
            <pattern>
                %d - %msg%n
            </pattern>
        </encoder>
        <!--滚动策略-->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--路径-->
            <fileNamePattern>/home/sudo_root/logs/api/error.%d.log</fileNamePattern>
        </rollingPolicy>
    </appender>


    <logger name="cn.xxx.mapper" level="DEBUG"/>

    <root level="INFO">
        <appender-ref ref="fileErrorLog" />
        <appender-ref ref="fileInfoLog" />
        <appender-ref ref="consoleLog" />
    </root>

</configuration>

项目中如果使用了mybatis或者mp,希望同时记录下sql语句执行,那么需要进行下面两个步骤:

mybatis-plus:
  configuration:
    map-underscore-to-camel-case: false
    log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl

另外需要在logback配置文件中加入:

<logger name="cn.xxx.mapper" level="DEBUG"/>

mapper下即为mybatis的接口所在位置

运行结果如下: