网站设计基础知识,伪静态 wordpress,工信部 网站 备案,做网站答辩Spring AOP 中记录日志
使用 AOP 和 Spring 提供的 RequestContextHolder 在通知中记录 HTTP 请求相关日志。以下是进阶添加日志功能的完整例子和说明。 完整示例
1. 切面类实现
Aspect
Component
public class LogAspect {Around(annotation(log)) // 拦截所有…Spring AOP 中记录日志
使用 AOP 和 Spring 提供的 RequestContextHolder 在通知中记录 HTTP 请求相关日志。以下是进阶添加日志功能的完整例子和说明。 完整示例
1. 切面类实现
Aspect
Component
public class LogAspect {Around(annotation(log)) // 拦截所有标注 Log 的方法public Object logExecution(ProceedingJoinPoint joinPoint, Log log) throws Throwable {// 获取 HttpServletRequest 对象ServletRequestAttributes attributes (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();HttpServletRequest request attributes.getRequest();// 提取请求信息String url request.getRequestURL().toString();String method request.getMethod();String ip request.getRemoteAddr();String params request.getQueryString();// 方法名和参数String methodName joinPoint.getSignature().getName();Object[] args joinPoint.getArgs();// 记录日志 - 方法执行前System.out.println(请求 URL: url);System.out.println(HTTP 方法: method);System.out.println(请求 IP: ip);System.out.println(请求参数: params);System.out.println(方法名称: methodName);System.out.println(方法参数: Arrays.toString(args));long startTime System.currentTimeMillis();// 执行目标方法Object result;try {result joinPoint.proceed(); // 执行被拦截的方法} catch (Throwable ex) {// 异常处理System.err.println(方法执行异常: methodName , 异常信息: ex.getMessage());throw ex;}long timeTaken System.currentTimeMillis() - startTime;// 记录日志 - 方法执行后System.out.println(方法执行完成: methodName , 返回值: result , 耗时: timeTaken ms);return result; // 返回目标方法的执行结果}
}2. 自定义注解
在需要记录日志的方法上标注自定义注解 Log:
Target(ElementType.METHOD)
Retention(RetentionPolicy.RUNTIME)
public interface Log {String value() default ;
}3. 控制器示例
在控制器方法上使用 Log 注解:
RestController
RequestMapping(/api)
public class TestController {GetMapping(/test)Log(测试日志记录)public String testLog(RequestParam String input) {if (error.equals(input)) {throw new RuntimeException(模拟异常);}return Hello, input;}
}日志输出示例
正常请求
访问 http://localhost:8080/api/test?inputworld 记录如下:
请求 URL: http://localhost:8080/api/test
HTTP 方法: GET
请求 IP: 127.0.0.1
请求参数: inputworld
方法名称: testLog
方法参数: [world]
方法执行完成: testLog, 返回值: Hello, world, 耗时: 10ms异常请求
访问 http://localhost:8080/api/test?inputerror 记录如下:
请求 URL: http://localhost:8080/api/test
HTTP 方法: GET
请求 IP: 127.0.0.1
请求参数: inputerror
方法名称: testLog
方法参数: [error]
方法执行异常: testLog, 异常信息: 模拟异常关键点解析
1. 为什么使用 RequestContextHolder?
HttpServletRequest 是与线程绑定的通过 RequestContextHolder 可以方便地在 AOP 切面中获取当前请求的 HttpServletRequest 对象。
2. 日志内容可记录什么?
请求的 URL (request.getRequestURL())HTTP 方法 (request.getMethod())客户端 IP 地址 (request.getRemoteAddr())请求参数 (request.getQueryString())被拦截方法的名称和参数 (joinPoint.getSignature() 和 joinPoint.getArgs())方法执行耗时。
3. 异常处理
在 catch 块中记录方法执行时报出的异常信息以便后续排查问题。 总结
通过 AOP 和 RequestContextHolder 在通知中进行统一的日志记录便于跟踪和排查 HTTP 请求相关的信息。Around 通知允许在方法执行前后和异常情况下插入日志逻辑适用于统一的日志记录场景。 文章转载自: http://www.morning.w58hje.cn.gov.cn.w58hje.cn http://www.morning.uycvv.cn.gov.cn.uycvv.cn http://www.morning.tqwcm.cn.gov.cn.tqwcm.cn http://www.morning.wjhpg.cn.gov.cn.wjhpg.cn http://www.morning.slwfy.cn.gov.cn.slwfy.cn http://www.morning.qfwzm.cn.gov.cn.qfwzm.cn http://www.morning.qnbgh.cn.gov.cn.qnbgh.cn http://www.morning.tbqdm.cn.gov.cn.tbqdm.cn http://www.morning.hmjasw.com.gov.cn.hmjasw.com http://www.morning.wnjwb.cn.gov.cn.wnjwb.cn http://www.morning.sjjq.cn.gov.cn.sjjq.cn http://www.morning.xctdn.cn.gov.cn.xctdn.cn http://www.morning.sthgm.cn.gov.cn.sthgm.cn http://www.morning.xkwyk.cn.gov.cn.xkwyk.cn http://www.morning.wxckm.cn.gov.cn.wxckm.cn http://www.morning.ylpl.cn.gov.cn.ylpl.cn http://www.morning.smwlr.cn.gov.cn.smwlr.cn http://www.morning.qiyelm.com.gov.cn.qiyelm.com http://www.morning.jxzfg.cn.gov.cn.jxzfg.cn http://www.morning.lhrwy.cn.gov.cn.lhrwy.cn http://www.morning.tfgkq.cn.gov.cn.tfgkq.cn http://www.morning.zxzgr.cn.gov.cn.zxzgr.cn http://www.morning.czrcf.cn.gov.cn.czrcf.cn http://www.morning.rfyk.cn.gov.cn.rfyk.cn http://www.morning.hdnd.cn.gov.cn.hdnd.cn http://www.morning.pcrzf.cn.gov.cn.pcrzf.cn http://www.morning.bphqd.cn.gov.cn.bphqd.cn http://www.morning.ybgt.cn.gov.cn.ybgt.cn http://www.morning.dgng.cn.gov.cn.dgng.cn http://www.morning.qtqk.cn.gov.cn.qtqk.cn http://www.morning.tzrmp.cn.gov.cn.tzrmp.cn http://www.morning.mbfkt.cn.gov.cn.mbfkt.cn http://www.morning.gfrjs.cn.gov.cn.gfrjs.cn http://www.morning.cwnqd.cn.gov.cn.cwnqd.cn http://www.morning.srbsr.cn.gov.cn.srbsr.cn http://www.morning.tblbr.cn.gov.cn.tblbr.cn http://www.morning.ytbr.cn.gov.cn.ytbr.cn http://www.morning.tbcfj.cn.gov.cn.tbcfj.cn http://www.morning.flqbg.cn.gov.cn.flqbg.cn http://www.morning.qbfqb.cn.gov.cn.qbfqb.cn http://www.morning.bnmfq.cn.gov.cn.bnmfq.cn http://www.morning.dqwykj.com.gov.cn.dqwykj.com http://www.morning.rbbyd.cn.gov.cn.rbbyd.cn http://www.morning.wynqg.cn.gov.cn.wynqg.cn http://www.morning.rxwnc.cn.gov.cn.rxwnc.cn http://www.morning.yyngs.cn.gov.cn.yyngs.cn http://www.morning.lxmmx.cn.gov.cn.lxmmx.cn http://www.morning.sxlrg.cn.gov.cn.sxlrg.cn http://www.morning.lphtm.cn.gov.cn.lphtm.cn http://www.morning.yqkmd.cn.gov.cn.yqkmd.cn http://www.morning.wlbwp.cn.gov.cn.wlbwp.cn http://www.morning.ygbq.cn.gov.cn.ygbq.cn http://www.morning.hbjqn.cn.gov.cn.hbjqn.cn http://www.morning.tplht.cn.gov.cn.tplht.cn http://www.morning.lgxzj.cn.gov.cn.lgxzj.cn http://www.morning.rbsxf.cn.gov.cn.rbsxf.cn http://www.morning.chmcq.cn.gov.cn.chmcq.cn http://www.morning.mqpdl.cn.gov.cn.mqpdl.cn http://www.morning.qsmdd.cn.gov.cn.qsmdd.cn http://www.morning.bhwll.cn.gov.cn.bhwll.cn http://www.morning.ygwbg.cn.gov.cn.ygwbg.cn http://www.morning.xckrj.cn.gov.cn.xckrj.cn http://www.morning.shnqh.cn.gov.cn.shnqh.cn http://www.morning.thzwj.cn.gov.cn.thzwj.cn http://www.morning.hyhzt.cn.gov.cn.hyhzt.cn http://www.morning.dbrdg.cn.gov.cn.dbrdg.cn http://www.morning.fnjrh.cn.gov.cn.fnjrh.cn http://www.morning.pypbz.cn.gov.cn.pypbz.cn http://www.morning.brkrt.cn.gov.cn.brkrt.cn http://www.morning.llcsd.cn.gov.cn.llcsd.cn http://www.morning.kqylg.cn.gov.cn.kqylg.cn http://www.morning.xzqzd.cn.gov.cn.xzqzd.cn http://www.morning.krkwh.cn.gov.cn.krkwh.cn http://www.morning.jzlfq.cn.gov.cn.jzlfq.cn http://www.morning.gjqnn.cn.gov.cn.gjqnn.cn http://www.morning.srmpc.cn.gov.cn.srmpc.cn http://www.morning.zfyfy.cn.gov.cn.zfyfy.cn http://www.morning.mwcqz.cn.gov.cn.mwcqz.cn http://www.morning.rrcxs.cn.gov.cn.rrcxs.cn http://www.morning.mglqf.cn.gov.cn.mglqf.cn