Spring工具包-log

Spring工具包日志组件,定义日志标准输出格式,方便排查线上问题

Posted by guanyang on 2022-06-08
Words 339 and Reading Time 1 Minutes
Viewed Times

概览

  • 针对所有请求,添加x-trace-id,方便追踪,参考LogInterceptor
  • 增加日志输出切面,方便排查方法调用日志,参考LogTraceAspect

使用指南

1
2
3
4
5
<dependency>
<groupId>org.gy.framework</groupId>
<artifactId>spring-base-log</artifactId>
<version>1.0.1-SNAPSHOT</version>
</dependency>

配置说明

  • 日志@LogTrace支持类、方法层级定义
  • 定义到类层级,会记录当前类所有方法调用日志,示例如下
    1
    2
    3
    4
    5
    6
    7
    @Slf4j
    @RestController
    @RequestMapping("/test")
    @LogTrace
    public class TestController {
    ...
    }
  • 定义到方法级别,仅记录当前方法调用日志,示例如下:
    1
    2
    3
    4
    5
    @GetMapping("/log")
    @LogTrace(fieldName = "dto", desc = "测试日志")
    public Response log(@Valid TestRequestDTO dto) {
    return testService.test(dto);
    }
  • 输出日志内容如下:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    {
    "invokeEndTime": 1638371312536,
    "responseBody": "{\"data\":{\"name\":null,\"time\":1638371312536},\"error\":0,\"msg\":\"success\",\"requestId\":\"e214d479e51042c5a8e139c1b974ade5\"}",
    "logStartTime": 1638371312111,
    "logEndTime": 1638371312537,
    "methodName": "test",
    "className": "org.gy.framework.demo.service.controller.TestController",
    "version": "v1",
    "requestBody": "{\"dto\":{\"name\":\"test\"}}",
    "clientIp": "127.0.0.1",
    "serverIp": "10.100.129.177",
    "invokeStartTime": 1638371312129,
    "invokeCostTime": 407,
    "desc": "default"
    }

If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !

...

...

00:00
00:00