1.0.2包,将控制台日志改为并发安全显示
This commit is contained in:
18
pom.xml
18
pom.xml
@@ -10,7 +10,7 @@
|
||||
</parent>
|
||||
<groupId>com.common.base</groupId>
|
||||
<artifactId>base</artifactId>
|
||||
<version>1.0.1-beta1</version>
|
||||
<version>1.0.2-beta1</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>common-base</name>
|
||||
<description>基础包</description>
|
||||
@@ -155,22 +155,6 @@
|
||||
<build>
|
||||
<finalName>common-base</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<configuration>
|
||||
<webResources>
|
||||
<resource>
|
||||
<!-- 原路径 -->
|
||||
<directory>${project.basedir}/lib/</directory>
|
||||
<targetPath>WEB-INF/lib/</targetPath>
|
||||
<includes>
|
||||
<include>**/*.jar</include>
|
||||
</includes>
|
||||
</resource>
|
||||
</webResources>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.mybatis.generator</groupId>
|
||||
<artifactId>mybatis-generator-maven-plugin</artifactId>
|
||||
|
||||
@@ -1,69 +0,0 @@
|
||||
package com.common.base.common;
|
||||
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.annotation.After;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Before;
|
||||
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.util.Enumeration;
|
||||
|
||||
/***
|
||||
* 通过Aop拦截访问接口(Controller)的URL,参数,IP等信息
|
||||
*/
|
||||
@Aspect
|
||||
@Component
|
||||
public class WebPatternlLog {
|
||||
|
||||
@Value("${show-controller-log}")
|
||||
public boolean flag;
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(WebPatternlLog.class);
|
||||
|
||||
//这个切点的表达式需要根据自己的项目来写
|
||||
@Pointcut("execution(public * com..*.controller..*(..))")
|
||||
public void log() {
|
||||
|
||||
}
|
||||
|
||||
@Before("log()")
|
||||
public void doBefore(JoinPoint joinPoint) {
|
||||
|
||||
if (flag) {
|
||||
//logger.info("aop doBefore..");
|
||||
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
HttpServletRequest request = attributes.getRequest();
|
||||
|
||||
//url
|
||||
logger.info("url={}", request.getRequestURI());
|
||||
|
||||
//method
|
||||
logger.info("method={}", request.getMethod());
|
||||
|
||||
//ip
|
||||
logger.info("ip={}", request.getRemoteAddr());
|
||||
|
||||
//类方法
|
||||
logger.info("classMethod={}", joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
|
||||
|
||||
//参数
|
||||
Enumeration<String> paramter = request.getParameterNames();
|
||||
while (paramter.hasMoreElements()) {
|
||||
String str = (String) paramter.nextElement();
|
||||
logger.info(str + "={}", request.getParameter(str));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@After("log()")
|
||||
public void doAfter() {
|
||||
//logger.info("aop doAfter");
|
||||
}
|
||||
}
|
||||
120
src/main/java/com/common/base/log/RequestLogAspect.java
Normal file
120
src/main/java/com/common/base/log/RequestLogAspect.java
Normal file
@@ -0,0 +1,120 @@
|
||||
package com.common.base.log;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.common.base.log.model.RequestErrorInfo;
|
||||
import com.common.base.log.model.RequestInfo;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.AfterThrowing;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Pointcut;
|
||||
import org.aspectj.lang.reflect.MethodSignature;
|
||||
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 org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* @author lhc
|
||||
*/
|
||||
@Component
|
||||
@Aspect
|
||||
public class RequestLogAspect {
|
||||
private final static Logger LOGGER = LoggerFactory.getLogger(RequestLogAspect.class);
|
||||
|
||||
@Pointcut("execution(public * com..*.controller..*(..))")
|
||||
public void requestServer() {
|
||||
}
|
||||
|
||||
@Value("${show-controller-log}")
|
||||
public boolean flag;
|
||||
|
||||
@Around("requestServer()")
|
||||
public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
|
||||
|
||||
long start = System.currentTimeMillis();
|
||||
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
HttpServletRequest request = attributes.getRequest();
|
||||
Object result = proceedingJoinPoint.proceed();
|
||||
|
||||
if (flag) {
|
||||
RequestInfo requestInfo = new RequestInfo();
|
||||
requestInfo.setIp(request.getRemoteAddr());
|
||||
requestInfo.setUrl(request.getRequestURL().toString());
|
||||
requestInfo.setHttpMethod(request.getMethod());
|
||||
requestInfo.setClassMethod(String.format("%s.%s", proceedingJoinPoint.getSignature().getDeclaringTypeName(),
|
||||
proceedingJoinPoint.getSignature().getName()));
|
||||
requestInfo.setRequestParams(getRequestParamsByProceedingJoinPoint(proceedingJoinPoint));
|
||||
requestInfo.setResult(result);
|
||||
requestInfo.setTimeCost(System.currentTimeMillis() - start);
|
||||
LOGGER.info("Request Info : {}", JSON.toJSONString(requestInfo));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@AfterThrowing(pointcut = "requestServer()", throwing = "e")
|
||||
public void doAfterThrow(JoinPoint joinPoint, RuntimeException e) {
|
||||
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
HttpServletRequest request = attributes.getRequest();
|
||||
RequestErrorInfo requestErrorInfo = new RequestErrorInfo();
|
||||
requestErrorInfo.setIp(request.getRemoteAddr());
|
||||
requestErrorInfo.setUrl(request.getRequestURL().toString());
|
||||
requestErrorInfo.setHttpMethod(request.getMethod());
|
||||
requestErrorInfo.setClassMethod(String.format("%s.%s", joinPoint.getSignature().getDeclaringTypeName(),
|
||||
joinPoint.getSignature().getName()));
|
||||
requestErrorInfo.setRequestParams(getRequestParamsByJoinPoint(joinPoint));
|
||||
requestErrorInfo.setException(e);
|
||||
LOGGER.info("Error Request Info : {}", JSON.toJSONString(requestErrorInfo));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取入参
|
||||
*
|
||||
* @param proceedingJoinPoint
|
||||
* @return
|
||||
*/
|
||||
private Map<String, Object> getRequestParamsByProceedingJoinPoint(ProceedingJoinPoint proceedingJoinPoint) {
|
||||
//参数名
|
||||
String[] paramNames = ((MethodSignature) proceedingJoinPoint.getSignature()).getParameterNames();
|
||||
//参数值
|
||||
Object[] paramValues = proceedingJoinPoint.getArgs();
|
||||
return buildRequestParam(paramNames, paramValues);
|
||||
}
|
||||
|
||||
private Map<String, Object> getRequestParamsByJoinPoint(JoinPoint joinPoint) {
|
||||
//参数名
|
||||
String[] paramNames = ((MethodSignature) joinPoint.getSignature()).getParameterNames();
|
||||
//参数值
|
||||
Object[] paramValues = joinPoint.getArgs();
|
||||
return buildRequestParam(paramNames, paramValues);
|
||||
}
|
||||
|
||||
private Map<String, Object> buildRequestParam(String[] paramNames, Object[] paramValues) {
|
||||
Map<String, Object> requestParams = new HashMap<>();
|
||||
for (int i = 0; i < paramNames.length; i++) {
|
||||
Object value = paramValues[i];
|
||||
if ((value instanceof HttpServletRequest) || (value instanceof HttpServletResponse)) {
|
||||
continue;
|
||||
}
|
||||
//如果是文件对象
|
||||
if (value instanceof MultipartFile) {
|
||||
MultipartFile file = (MultipartFile) value;
|
||||
// 获取文件名
|
||||
value = file.getOriginalFilename();
|
||||
}
|
||||
requestParams.put(paramNames[i], value);
|
||||
}
|
||||
return requestParams;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.common.base.log.model;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @author lhc
|
||||
*/
|
||||
@Data
|
||||
public class RequestErrorInfo implements Serializable {
|
||||
private static final long serialVersionUID = 2671491007419047891L;
|
||||
private String ip;
|
||||
private String url;
|
||||
private String httpMethod;
|
||||
private String classMethod;
|
||||
private Object requestParams;
|
||||
private RuntimeException exception;
|
||||
}
|
||||
21
src/main/java/com/common/base/log/model/RequestInfo.java
Normal file
21
src/main/java/com/common/base/log/model/RequestInfo.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package com.common.base.log.model;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
|
||||
/**
|
||||
* @author lhc
|
||||
*/
|
||||
@Data
|
||||
public class RequestInfo implements Serializable {
|
||||
private static final long serialVersionUID = 386274939008099288L;
|
||||
private String ip;
|
||||
private String url;
|
||||
private String httpMethod;
|
||||
private String classMethod;
|
||||
private Object requestParams;
|
||||
private Object result;
|
||||
private Long timeCost;
|
||||
}
|
||||
Reference in New Issue
Block a user