@Target(ElementType.METHOD) //注解放置的目标位置,METHOD是可注解在方法级别上
@Retention(RetentionPolicy.RUNTIME) //注解在哪个阶段执行
@Documented
public @interface SysLog {
String value() default "";
}
@Aspect
@Component
@Slf4j
public class AspectLog {
ThreadLocal<Long> startTime = new ThreadLocal<Long>();
@Pointcut("@annotation(com.jysp.core.annotation.SysLog)")
public void logPointCut() {
}
@Before(value = "logPointCut()")
public void methodBefore(JoinPoint joinPoint) {
//开始时间
startTime.set(System.currentTimeMillis());
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();
//打印请求内容
log.info("===============请求内容===============");
log.info("请求地址:" + request.getRequestURL().toString());
log.info("请求方式:" + request.getMethod());
log.info("请求类方法:" + joinPoint.getSignature());
String args = joinPoint.getArgs() != null ? Arrays.toString(joinPoint.getArgs()) : "";
log.info("请求类方法参数:" + args);
log.info("=======================================");
}
@AfterReturning(returning = "o", pointcut = "logPointCut()")
public void methodAfterReturning(Object o) {
//打印返回内容
log.info("--------------返回内容----------------");
String responseContent = o != null ? JSON.toJSONString(o) : "";
log.info("Response内容:" + responseContent);
//打印响应时间
log.info("--------------响应时间----------------");
log.info("请求处理时间为:" + (System.currentTimeMillis() - startTime.get()) + "毫秒");
log.info("=======================================");
}
}
@SysLog
@PostMapping("/test")
public ResponseBean<String> test(@RequestBody Test dto) {
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现