SpringBoot 系列实战 | 第三篇: SpringBoot项目实现AOP切面环绕通知记录日志信息
一、配置启动对@AspectJ注解的支持及监听类
package lzq.boot.test.log;
import lzq.boot.test.utils.IpAdrressUtil;
import lzq.boot.test.utils.JsonUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* @ClassName SysLogAspect
* @Description 系统日志:切面处理类
*/
@Aspect
@Component
public class SysLogAspect {
//项目自身IP地址端口
@Value("${eureka.instance.instance-id}")
private String zip;
//项目端口
@Value("${server.port}")
private String port;
private static final Logger log = LoggerFactory.getLogger(SysLogAspect.class);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.ssssss");
//保存普通请求日志信息Map
Map<String, Object> logInfoMap = new ConcurrentHashMap<String, Object>();
//普通日志 定义切点 在注解的位置切入代码
@Pointcut("execution(public * lzq.boot.test.controller..*.*(..))")
public void logPoinCut() {
}
/**
* 普通日志—环绕通知
*/
@Around("logPoinCut()")
public Object doAfterReturning(ProceedingJoinPoint joinPoint) throws Throwable {
logInfoMap.clear();
//获取开始时间
long logStartTime = System.currentTimeMillis();
logInfoMap.put("开始时间", sdf.format(logStartTime));
Object resultObject = null;
try {
//项目自身IP地址端口
logInfoMap.put("自身IP地址", zip);
//获取请求的类名全路径
logInfoMap.put("请求接口项目路径", joinPoint.getTarget().getClass().getName());
//获取切入点所在的方法
logInfoMap.put("方法名", joinPoint.getSignature().getName());
//获取请求参数
logInfoMap.put("参数", JsonUtils.beanToJson(joinPoint.getArgs()));
//获取用户ip地址
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
logInfoMap.put("请求接口地址", IpAdrressUtil.getIpAddr(request) + ":" + port + request.getRequestURI());
//请求返回数据
resultObject = joinPoint.proceed();
logInfoMap.put("返回数据", JsonUtils.beanToJson(resultObject));
} catch (Throwable throwable) {
logInfoMap.put("异常信息", throwable.getMessage());
throw throwable;
} finally {
long logEndTime = System.currentTimeMillis(); //获取结束时间
logInfoMap.put("结束时间", sdf.format(logEndTime));
logInfoMap.put("总耗时", logEndTime - logStartTime + "毫秒");
log.info(joinPoint.getSignature().getName() + "方法日志:{}", logInfoMap.toString());
}
return resultObject;
}
}
二、IpAdrressUtil类
//获取用户ip地址
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
logInfoMap.put("请求接口地址", IpAdrressUtil.getIpAddr(request) + ":" + port + request.getRequestURI());
IpAdrressUtil.getIpAddr()方法详见:Java常用工具类获取IP地址IpAdrressUtil
三、JsonUtils类
//请求返回数据
resultObject = joinPoint.proceed();
logInfoMap.put("返回数据", JsonUtils.beanToJson(resultObject));
JsonUtils.beanToJson()方法详见:Java常用工具类JsonUtils
四、项目配置Swagger2
项目配置Swagger2详见:SpringBoot 系列实战 | 第一篇: SpringBoot项目整合Swagger2(SpringBoot+Swagger2)(2.6.1版本)
五、项目结构
六、启动项目进行测试验证
浏览器访问:http://127.0.0.1:8083/swagger-ui.html
控制台输出日志信息:
2020-11-18 14:54:52.730 INFO 1836 --- [nio-8083-exec-9] lzq.boot.test.log.SysLogAspect : getTest方法日志:{请求接口地址=192.168.227.1:8083/getTest, 总耗时=2毫秒, 开始时间=2020-11-18 14:54:52.000052, 参数=["测试"], 方法名=getTest, 返回数据="调用成功:测试", 结束时间=2020-11-18 14:54:52.000052, 自身IP地址=192.168.43.144:8083, 请求接口项目路径=lzq.boot.test.controller.TestController}