Springboot的sql日志打印与AOP实现接口调用日志打印
` 1、Springboot与mybatis或者mybatisplus 打印sql执行语句。使用的是springboot中内部嵌入的日志框架。
2、接口调用日志,主要是区分当前接口的调用情况。其中包括调用接口的url,接口名称,调用方式,入参和出参等数据`
例如:第一章 Python 机器学习入门之pandas的使用
文章目录
- Springboot的sql日志打印与AOP实现接口调用日志打印
- 一、打印sql查询日志
- 二、打印接口调用日志
一、打印sql查询日志
1、在application.yml中配置
(1)配置日志打印级别
logging:
level:
root: INFO
(2)mybtais或者mybatisplus 配置 打印sql
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
二、打印接口调用日志
1、目标:打印调用接口的url,传入参数,出参,运行时间
2、实现方式:使用AOP切面编程的方式 打印接口调用日志
3、引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
4、切面编程的代码
package com.example.sso.tool;
import com.alibaba.fastjson.JSONObject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
@Aspect
@Component
public class WebLogAspect {
private final static Logger logger = LoggerFactory.getLogger(WebLogAspect.class);
@Pointcut("execution(public * com.example.sso.controller..*.*(..))")
public void webLog() {}
@Before("webLog()")
public void doBefore(JoinPoint joinPoint) throws Throwable {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
logger.info("========================================== Start ==========================================");
logger.info("URL : {}", request.getRequestURL().toString());
logger.info("HTTP Method : {}", request.getMethod());
logger.info("Class Method : {}.{}", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName());
String s = JSONObject.toJSONString(joinPoint.getArgs());
logger.info("Into Parameters: {}", s );
}
@After("webLog()")
public void doAfter() throws Throwable {
logger.info("=========================================== End ===========================================");
logger.info("");
}
@Around("webLog()")
public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
long startTime = System.currentTimeMillis();
Object result = proceedingJoinPoint.proceed();
String s = JSONObject.toJSONString(result.toString());
logger.info("Return Results : {}", s);
logger.info("Time-Consuming : {} ms", System.currentTimeMillis() - startTime);
return result;
}
}
Aop切面编程 请自行百度了解
代码里里面有fastjson 的使用,所以在添加依赖的时候 需要添加fastjson 的依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<!--(起码1.2.48以上)因为这个版本一下存在漏洞-->
<version>1.2.58</version>
</dependency>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)