com.github.lhnonline.boot.common.aop.HttpRequestLogAop Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of weboot-common Show documentation
Show all versions of weboot-common Show documentation
用于SpringBoot项目 | 接口返回数据包装,http状态码aop, http请求日志统一打印 | 使用了fastjson,和 mybatis-plus-extension
The newest version!
package com.github.lhnonline.boot.common.aop;
import com.baomidou.mybatisplus.core.toolkit.ArrayUtils;
import com.github.lhnonline.boot.common.log.AbstractLog;
import com.github.lhnonline.boot.common.log.DefaultLog;
import com.github.lhnonline.boot.common.log.ILogWriter;
import com.github.lhnonline.boot.common.properties.RequestLogConfig;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* author luohaonan
* date 2020-11-11
* email [email protected]
* description http请求日志记录切面
*/
@Aspect
@Slf4j
@SuppressWarnings("all")
public class HttpRequestLogAop {
@Autowired
private RequestLogConfig config;
@Autowired
private ILogWriter logWriter;
public void doBefore(JoinPoint joinPoint, AbstractLog logInfo) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attributes == null) {
return;
}
HttpServletRequest request = attributes.getRequest();
logInfo.setRequestUri(request.getRequestURL().toString());
logInfo.setRequestMethod(request.getMethod());
if (config.getHeaderEnabled()) {
if (config.getHeaderAll()) {
Enumeration headerNames = request.getHeaderNames();
logInfo.setHeaderNames(headerNames);
while (headerNames.hasMoreElements()) {
String h = headerNames.nextElement();
logInfo.getHeaderValues().put(h, request.getHeader(h));
}
} else if (config.getLogHeaderSet().isEmpty()) {
for (String headerName : config.getLogHeaderSet()) {
String value = request.getHeader(headerName);
logInfo.getHeaderValues().put(headerName, request.getHeader(headerName));
}
}
}
logInfo.setMethodSignature(joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
/////////////////////////////////////////////////////////////////////////////////////
Object[] args = joinPoint.getArgs();
Stream> stream = ArrayUtils.isEmpty(args) ? Stream.empty() : Arrays.asList(args).stream();
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy