1. 新增pom依赖

      1. <dependency>
      2. <groupId>io.github.mouzt</groupId>
      3. <artifactId>bizlog-sdk</artifactId>
      4. <version>1.0.1</version>
      5. </dependency>
    2. 启动项Application,添加注解@EnableLogRecord(tenant = “xxx.xxx”)

    tenant 代表租户标识,可自由定义

    1. @SpringBootApplication
    2. @EnableLogRecord(tenant = "com.test.demo")
    3. @Slf4j
    4. public class StudyProjectApplication {
    5. public static void main(String[] args) {
    6. SpringApplication.run(StudyProjectApplication.class, args);
    7. log.info("启动成功!");
    8. }
    9. }

    image.png

    1. 在接口或实现类上添加注解@LogRecordAnnotation,有如下属性:
    • prefix 用以区分业务
    • bizNo 业务id
    • success 成功打印的msg
    • fail 失败打印的msg
    • operator 操作人
    • category 用以区分日志类型,比如有些是运营人员的操作日志,不想让系统暴露
    • detail 可拿到接口传进来的对象参数
    • SpEL 表达式 {{#A.b}}表示参数,eg:{{#devicePoint.toString()}}
      1. /**
      2. * pojo插入,measurement即Pojo
      3. *
      4. * @param devicePoint
      5. * @return
      6. */
      7. @LogRecordAnnotation(prefix = "influxDB",
      8. detail = "{{#devicePoint.toString()}}",
      9. bizNo = "{{#devicePoint.name}}",
      10. success = "新增成功{{#devicePoint.name}},下单结果:{{#_ret}}",
      11. fail = "新增失败,「{{#_errorMsg}}」",
      12. operator = "{{#currentUser}}",
      13. category = "MANAGER")
      14. @PostMapping("/insertPojo")
      15. public String insertPojo(@RequestBody DevicePoint devicePoint) {
      16. Point.Builder builder = Point.measurementByPOJO(devicePoint.getClass());
      17. builder.addFieldsFromPOJO(devicePoint);
      18. builder.tag("no", String.valueOf(UUID.randomUUID()));
      19. influxDB.write(builder.build());
      20. return "成功";
      21. }
    1. 执行结果

    image.png
    成功日志:

    1. logRecordlog=LogRecord(id=null, tenant=com.test.demo, bizKey=influxDB_device#3, bizNo=device#3, operator=111, action=新增成功device#3,下单结果:成功, category=MANAGER, createTime=Fri Feb 26 14:13:44 CST 2021, detail=DevicePoint(name=device#3, time=null, no=123, value=3cm))

    失败日志:

    1. logRecordlog=LogRecord(id=null, tenant=com.test.demo, bizKey=influxDB_device#3, bizNo=device#3, operator=111, action=新增失败,「/ by zero」, category=MANAGER, createTime=Fri Feb 26 14:21:31 CST 2021, detail=DevicePoint(name=device#3, time=null, no=123, value=3cm))

    原文:https://mp.weixin.qq.com/s/HJ8XWb7fkv9tZkSTVFOfbw