package top.mytao.myproject.platform.aspact;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
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.bouncycastle.asn1.ocsp.ResponseData;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import top.mytao.myproject.business.event.LogEvent;
import top.mytao.myproject.platform.Constants;
import top.mytao.myproject.platform.controller.entity.LogEntity;
import top.mytao.myproject.platform.enums.SysExpEnum;
import top.mytao.myproject.platform.util.IpAddressUtil;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;
/**
* AOP 统一处理日志信息
*
* @author
* @date 2022/1/15
*/
@Slf4j
@Component
@Aspect
public class WebLogAspect {
public static final String GET = "GET";
@Autowired
private ApplicationContext context;
/**
* 定义切点
*/
@Pointcut("execution(public * top.mytao.myproject.platform.controller.BaseController+.*(..))")
public void requestServer() {
}
@Around("requestServer()")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
// 请求开始时间
long startTime = System.currentTimeMillis();
try {
// 处理请求
Object result = pjp.proceed();
// 成功处理
processSuccess(startTime, pjp, result);
return result;
} catch (Exception e) {
log.error("http request failed!", e);
// 异常处理
processError(startTime, pjp, e.getMessage());
throw e;
}
}
private LogEntity buildLogEntity(ProceedingJoinPoint pjp, long startTime, String returnCode) {
// 获取请求信息
ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = sra.getRequest();
// 获取代理地址、请求地址、请求类名、方法名
LogEntity logEntity = new LogEntity();
logEntity.setIp(IpAddressUtil.getIpAdrress(request));
logEntity.setRequestUrl(request.getRequestURI());
logEntity.setLogId(MDC.get(Constants.TRACE_ID));
logEntity.setHttpMethod(request.getMethod());
logEntity.setTargetClass(pjp.getTarget().getClass().getSimpleName());
logEntity.setTargetMethod(pjp.getSignature().getName());
logEntity.setRequestParams(getRequestParams(pjp, request));
logEntity.setStartTime(startTime);
logEntity.setReturnCode(returnCode);
// 请求完成时间
long endTime = System.currentTimeMillis();
logEntity.setCost(endTime - startTime);
logEntity.setEndTime(endTime);
return logEntity;
}
private String getRequestParams(JoinPoint joinPoint, HttpServletRequest request) {
String requestParams;
if (GET.equals(request.getMethod())) {
requestParams = request.getQueryString();
}else {
// 获取参数信息
requestParams = JSONObject.toJSONString(joinPoint.getArgs());
}
return requestParams;
}
private void processError(long startTime, ProceedingJoinPoint pjp, String errorMessage) {
LogEntity logEntity = buildLogEntity(pjp, startTime, SysExpEnum.UNKNOW_ERROR.getCode());
logEntity.setErrorInfo(errorMessage);
// 推送日志到数据库
pushLog(logEntity);
}
private void processSuccess(long startTime, ProceedingJoinPoint pjp, Object result) {
LogEntity logEntity = buildLogEntity(pjp, startTime, SysExpEnum.SUCCESS.getCode());
// 记录请求完成执行时间
String response = getResponse(result);
logEntity.setResponse(response);
// 推送日志
pushLog(logEntity);
}
private void pushLog(LogEntity logEntity) {
LogEvent.Builder logEvent = new LogEvent.Builder(this).logEntity(logEntity);
context.publishEvent(logEvent.build());
log.info(JSON.toJSONString(logEntity));
}
private String getResponse(Object result) {
String response = "";
if (Objects.isNull(result)) {
return response;
}
if (result instanceof ResponseData) {
response = JSON.toJSONString(result, SerializerFeature.WriteDateUseDateFormat);
}else {
response = String.valueOf(result);
}
return response;
}
}
AOP 统一处理后端日志
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- AOP统一处理请求日志 AOP为Aspect Oriented Programming的缩写,意为:[面向切面编程...
- SpringCloud(第 048 篇)使用AOP统一处理Web请求日志 一、大致介绍 二、实现步骤 2.1 添加...