接口调用的日志打印

 我在打印日志时在调用接口的前面和后面都打印了日志这样方便排除,接口调用有没有成功,也防止接口调用失败就无法打印下面的日志了。并且调用成功后不仅要打印出参也要打印入参这是为了,加快出现错误时故障的排除效率。另外捕获的异常要设置成Throwable 这个顶级父类,这样就不会出现有错误捕获不到的情况了。此外,出现错误时要打印入参信息,这样可以更快速的找到日志解决错误,不然我们仅能凭借时间戳来找错误日志,这样效率太低。如果是并发量比较大的系统,找错误都够你找好久了,更别提修复bug了。

try{
    log.info("调用远程接口入参为{}",s)   
    R r = userService.testException(String s);
    log.info("调用远程接口入参为{},出参为{}",s,r)   
}catch (Throwable t){
   log.error("发生错误啦,入参为{},错误详情:{}",s,t);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
你可以使用 @Slf4j 注解和 AOP 统一处理打印日志,具体实现可参考以下代码: 1. 引入依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> </dependency> ``` 2. 在应用主类上添加 @EnableAspectJAutoProxy 注解启用 AOP: ```java @SpringBootApplication @EnableAspectJAutoProxy(proxyTargetClass = true) public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 3. 定义切面类: ```java @Aspect @Component @Slf4j public class LogAspect { @Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)" + "||@annotation(org.springframework.web.bind.annotation.GetMapping)" + "||@annotation(org.springframework.web.bind.annotation.PostMapping)" + "||@annotation(org.springframework.web.bind.annotation.PutMapping)" + "||@annotation(org.springframework.web.bind.annotation.DeleteMapping)") public void webLog() { } @Before("webLog()") public void doBefore(JoinPoint joinPoint) { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); // 记录请求内容 log.info("URL : " + request.getRequestURL().toString()); log.info("HTTP_METHOD : " + request.getMethod()); log.info("IP : " + request.getRemoteAddr()); // 记录调用方法 log.info("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName()); // 记录请求参数 log.info("ARGS : " + Arrays.toString(joinPoint.getArgs())); } @AfterReturning(returning = "ret", pointcut = "webLog()") public void doAfterReturning(Object ret) { // 记录响应内容 log.info("RESPONSE : " + ret); } } ``` 4. 在需要打印日志接口方法上添加 @RequestMapping 等注解即可。 压缩文件的操作请参考以下代码: ```java public static void zip(Path sourcePath, Path zipPath) throws IOException { try (ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(zipPath)); Stream<Path> paths = Files.walk(sourcePath)) { paths.filter(p -> !Files.isDirectory(p)) .forEach(p -> { ZipEntry entry = new ZipEntry(sourcePath.relativize(p).toString()); try { zos.putNextEntry(entry); zos.write(Files.readAllBytes(p)); zos.closeEntry(); } catch (IOException e) { e.printStackTrace(); } }); } } ``` 对于中文加密的问题,我不是很确定您要表达的意思,请再提供更详细的问题描述。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小海海不怕困难

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值