默认输出日志打印,对往后续运维至关重要,请求一个Restfull接口的时,顺便打印些重要信息保存到日志记录
下面做一个简单的例子,可根据项目的调整 :
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
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 javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @Auther: 天够黑
* @Date: 2020/03/10 22:00
* @Description: AOP实现接口请求参数和响应参数的日志打印
*/
@Aspect
@Order(5)
@Component
@Slf4j
public class DefaultLogAop {
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/**
* 匹配需要打印的方法
* 1)execution(* *(..))
* //表示匹配所有方法
* 2)execution(public * com. item.service.UserService.*(..))
* //表示匹配com.item.server.UserService中所有的公有方法
* 3)execution(* com.item.server..*.*(..))
* //表示匹配com.item.server包及其子包下的所有方法
*/
@Pointcut("execution(public * com.item.controller..*.*(..))")
public void defultLog() {
}
@Before("defultLog()")
public void doBefore(JoinPoint joinPoint) {
// 接收到请求,记录请求内容
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
StringBuffer sb = new StringBuffer();
// 记录下请求内容
sb.append("--url: ").append(request.getRequestURL()).append(" --ip: ")
.append(request.getRemoteAddr()).append(" --method: ")
.append(joinPoint.getSignature().getDeclaringTypeName()).append(".")
.append(joinPoint.getSignature().getName()).append("访问开始时间:" + sdf.format(new Date()));
// 获取参数, 只取自定义的参数, 自带的HttpServletRequest, HttpServletResponse不管
if (joinPoint.getArgs().length > 0) {
for (Object o : joinPoint.getArgs()) {
if (o instanceof HttpServletRequest || o instanceof HttpServletResponse) {
continue;
}
sb.append("--params: ").append(JSON.toJSONString(o));
log.info(sb.toString());
}
}
}
@AfterReturning(returning = "ret", pointcut = "defultLog()")
public void doAfterReturning(Object ret) {
// 处理完请求,返回内容
log.info("--response : " + JSON.toJSONString(ret) + "访问结束时间:" + sdf.format(new Date()));
}
}
测试结果:
2020-02-26 23:22:29.653 INFO com.item.config.DefaultLogAop Line:58 - --response : {"code":200,"data":[{"accordAmount":50.00,"couponId":"7172856832","couponName":"满50元减5元","couponType":1,"discountAmount":5.00,"endTime":"1585666800000","goodsType":2,"isReceice":0,"startTime":"1581004800000","usageRule":"满50元减5元"}],"message":"请求处理完成"}访问结束时间:2020-02-26 23:22:29
2020-02-26 23:22:30.960 INFO com.item.config.DefaultLogAop Line:50 - --url: http://192.168.90.84:8080/api/coupon/xxx --ip: 192.168.90.93 --method: com.item.controller.xxx 访问开始时间:2020-02-26 23:22:30--params: {"storeId":"2799","systemId":"1001"}
对你有帮助的话,点个赞呗~