不多废话,直接上效果
2020-10-19 16:34:17.121 [http-nio-80-exec-2] �[34mINFO �[0;39m c.mycompany.settings.spring.WebLogAspect : ++++++++REQUEST URL : GET http://domain/my-service/v1/device/settings
2020-10-19 16:34:17.122 [http-nio-80-exec-2] �[34mINFO �[0;39m c.mycompany.settings.spring.WebLogAspect : ++++++++IP : 172.20.0.42
2020-10-19 16:34:17.122 [http-nio-80-exec-2] �[34mINFO �[0;39m c.mycompany.settings.spring.WebLogAspect : ++++++++CLASS_METHOD : com.mycompany.settings.core.DeviceSettingsController.list([DeviceSettingsCommand(deviceTypeId=ME, planId=1, deviceVersion=1.0.1, xxxVersion=1.8.1)])
2020-10-19 16:34:17.181 [http-nio-80-exec-2] �[34mINFO �[0;39m c.mycompany.settings.spring.WebLogAspect : +++++++++RESPONSE : [DeviceSettingsRepresentation(featureCode=cmd_timezone_40, featureName=Set Timezone, featureLabel=Set Timezone, featureDesc=null, uri=null, xxxSupportVersion=null, featureType=null, available=false, reason=This feature is currently in development, and will be available in a future release.), DeviceSettingsRepresentation(featureCode=cmd_camera_framerate_39, featureName=Camera Framerate, featureLabel=Camera Framerate, featureDesc=The xxx system automatically switches the Cam current refresh frequency based on the device's local time zone. It's not always accurate, though, and you can try doing a switch between 50Hz and 60Hz to fix the problem when your device's screen appears spent., uri=null, xxxSupportVersion=null, featureType=null, available=false, reason=This feature is currently in development, and will be available in a future release.), DeviceSettingsRepresentation(featureCode=cmd_virtual_background_38, featureName=Virtual Background, featureLabel=Virtual Background, feature...略
2020-10-19 16:34:17.181 [http-nio-80-exec-2] �[34mINFO �[0;39m c.mycompany.settings.spring.WebLogAspect : ++++++++SPEND TIME : 60 ms
步骤很简单,直接添加一个aspect类即可,直接上代码
注意:@Pointcut注解,正确填写controller的路径,否则不生效
import com.mycompay.myproject.common.constant.AppCons;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
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.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Optional;
/**
* controller日志切面类
* Created by author on 2018/12/1.
*/
@Slf4j
@Aspect
@Component
public class WebLogAspect {
ThreadLocal<Long> startTime = new ThreadLocal<>();
@Pointcut("execution(public * com.mycompany.settings.controller..*.*(..))")
public void webLog(){}
@Before("webLog()")
public void doBefore(JoinPoint joinPoint) {
startTime.set(System.currentTimeMillis());
// 接收到请求,记录请求内容
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
// 打印请求信息
log.info("++++++++REQUEST URL : {} {}", request.getMethod(), request.getRequestURL().toString());
log.info("++++++++IP : {}", request.getRemoteAddr());
log.info("++++++++CLASS_METHOD : {}.{}({})", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName(), Arrays.toString(joinPoint.getArgs()));
}
@AfterReturning(returning = "ret", pointcut = "webLog()")
public void doAfterReturning(Object ret) {
// 处理完请求,返回内容和响应时间
String respContent = Optional.ofNullable(ret).map(Object::toString).orElse("");
log.info("+++++++++RESPONSE : {}{}", StringUtils.substring(respContent,0, AppCons.LENGTH_OF_WEB_LOG),
respContent.length() > AppCons.LENGTH_OF_WEB_LOG ? "...略" : "");
log.info("++++++++SPEND TIME : {} ms", (System.currentTimeMillis() - startTime.get()));
startTime.remove();
}
}