日志切面配置按如下3个步骤进行
1.LogAop文件内容如下:
package com.ahunicom.parking.config;
import com.alibaba.fastjson.JSONObject;
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.context.annotation.Configuration;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
@Aspect
@Configuration
public class LogAop {
private static final Logger logger = LoggerFactory.getLogger(LogAop.class);
@Pointcut(value = "execution(public * com.ahunicom.parking.controller..*(..))")
public void execute(){
}
@Around("execute()")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)requestAttributes;
HttpServletRequest httpServletRequest = servletRequestAttributes.getRequest();
String uri = httpServletRequest.getRequestURI();
String method = httpServletRequest.getMethod();
String queryString = httpServletRequest.getQueryString();
Object[] args = pjp.getArgs();
String params = "";
if(args.length > 0){
if("POST".equals(method) ){
for(Object o:args){
if(null!=o){
Map map = getKeyAndValue(o);
if(!map.isEmpty()){
params += JSONObject.toJSONString(map);
}
}
}
}else if("GET".equals(method)){
params = queryString;
}
}
long beginTime = System.currentTimeMillis();
logger.info("请求URI:{},方法:{},参数:{}",uri,method,params);
Object result = pjp.proceed();
logger.info("请求结束,返回参数:{}",JSONObject.toJSONString(result));
long endTime = System.currentTimeMillis();
long time = endTime - beginTime;
logger.info("执行时长:{}mm",time);
return result;
}
public static Map getKeyAndValue(Object object){
Map map = new HashMap();
Class uCla = (Class)object.getClass();
if(!(uCla.getPackage().getName().startsWith("com.ahunicom")
|| uCla.getPackage().getName().startsWith("java.") )){
return map;
}
Field[] fs = uCla.getDeclaredFields();
for(int i=0;i
2.logback文件配置如下:
${log.pattern}
${log.path}/info.log
${log.path}/info.%d{yyyy-MM-dd}.log
7
${log.pattern}
INFO
ACCEPT
DENY
${log.path}/error.log
${log.path}/error.%d{yyyy-MM-dd}.log
7
${log.pattern}
ERROR
ACCEPT
DENY
3.pom.xml文件添加如下依赖:
org.springframework.boot
spring-boot-starter-aop